J2EE 过滤器:登录页面无法加载 css 或任何图像

Posted

技术标签:

【中文标题】J2EE 过滤器:登录页面无法加载 css 或任何图像【英文标题】:J2EE filter : login page unable to load the css or any images 【发布时间】:2012-10-01 18:01:12 【问题描述】:

我已经访问过Java FilterImplementation for session checking 链接,其中介绍了 Spring 安全性。我没有得到我需要的帮助。

应用过滤器 login.jsp 后无法加载 CSS 和图像。

我正在尝试在 web.xml 中提供过滤器并将过滤器应用于除 login.jsp 以外的页面的简单示例。 Web.xml 文件是:

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
 <filter>
 <filter-name>AuthenticationFilter2</filter-name>
 <filter-class>filter.AuthorizationFilter2</filter-class>
 <init-param>
    <param-name>avoid-urls</param-name>
    <param-value>login.jsp</param-value>
 </init-param>`
 <filter>

过滤器类是:

    private ArrayList<String> urlList;

public void destroy() 
    // TODO Auto-generated method stub
    System.out.println("authorization filter2 destroy method....");





public void doFilter(ServletRequest req, ServletResponse res,
        FilterChain chain) throws IOException, ServletException 

    System.out.println("authorization filter2 doFilter method....");

    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    String url = request.getServletPath();

    System.out.println("ppp:"+request.getRequestURL());

    System.out.println("url is :"+url);

    boolean allowedRequest = false;

    System.out.println("url list is :"+urlList);

    if(urlList.contains(url.substring(1))) 
        allowedRequest = true;
    
    System.out.println("request allowed....."+allowedRequest);     
    if (!allowedRequest) 

        Map session = ActionContext.getContext().getSession();

        /*HttpSession session = request.getSession(false);*/
       /* if (null == session) 
            response.sendRedirect("login.jsp");
        */

        System.out.println("session contains login :"+session.containsKey("login"));



        if(!session.containsKey("login"))
            response.sendRedirect("login.jsp");
        

    

    chain.doFilter(req, res);


public void init(FilterConfig config) throws ServletException 
    System.out.println("authorization filter2 init method....");
    String urls = config.getInitParameter("avoid-urls");
    StringTokenizer token = new StringTokenizer(urls, ",");

    urlList = new ArrayList<String>();

    while (token.hasMoreTokens()) 
        urlList.add(token.nextToken());

    

登录页面根据要求包含cssimages

请帮帮我。谢谢。

【问题讨论】:

【参考方案1】:
@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException 


    String uri = ((HttpServletRequest)request).getRequestURI();

    @SuppressWarnings("rawtypes")
    Map session = ActionContext.getContext().getSession();

    if ( uri.indexOf("/css") > 0)
        chain.doFilter(request, response);
    
    else if( uri.indexOf("/images") > 0)
        chain.doFilter(request, response);
    
    else if( uri.indexOf("/js") > 0)
        chain.doFilter(request, response);
    
    else if (session.containsKey("login")) 
        chain.doFilter(request, response);
    
    else 
        ((HttpServletResponse)response).sendRedirect(((HttpServletRequest)request).getContextPath() + "/login?authentication=failed");
    

将此代码块放入您的操作类中,它可以工作。 谢谢大家。

【讨论】:

【参考方案2】:

您在 logjn.jsp 页面上使用的 CSS 文件和图像必须从您的过滤器中排除

【讨论】:

url = request.getServletPath();过滤器类中的上述语句给出了奇怪的结果,例如: /images/login.jsp /images/test.png /css/login.jsp 虽然 url 是 login.jsp 这是因为您的 response.sendRedirect("login.jsp") 包含相对路径。见docs.oracle.com/javaee/1.4/api/javax/servlet/http/…。因此,当浏览器尝试获取一些 CSS 文件(例如 /css/someFile.css)时,过滤器会将其重定向到 /css/login.jsp。使用 FireBug 或 Chrome 开发者工具查看来自浏览器的所有请求 - 这将帮助您了解正在发生的事情。 你是对的,当我点击 login.jsp 网址时,它在内部点击了许多其他网址,如 css/login.jsp、images/login.jsp 但我无法找到它的解决方案请你帮助我。谢谢 您应该在过滤器中排除 css 和图像目录,或者更改 web.xml 文件中的过滤器映射,以便您的过滤器不会应用于这些位置。 ru 谈论这样的事情,AuthenticationFilter2filter.AuthorizationFilter2avoid-urls login.jsp, *.css, *.js, *.png`

以上是关于J2EE 过滤器:登录页面无法加载 css 或任何图像的主要内容,如果未能解决你的问题,请参考以下文章

j2ee webapp A 使用 web 应用 B 的身份验证工具

不安全登录被阻止:您无法获取访问令牌或从不安全页面登录到此应用程序。尝试将页面重新加载为 https://

Facebook 登录页面为空,无法加载

使用过滤器来限制 JSP 访问?

jsp过滤,获取页面名称

无法在 html 页面中加载 Css 文件