4

'm working on a JSF 2 project. I have defined my login.xhtml page as the entry page in web.xml

    <welcome-file-list>
        <welcome-file>login.xhtml</welcome-file>
    </welcome-file-list>

And I also have a filter to check if user is logged in

    @WebFilter(filterName = "loginCheckFilter", urlPatterns={"/*"})
    public class LoginCheckFilter implements Filter
    {
        @Inject
        private LoginStatus loginStatus;

        public void do Filter(...)
        {
            try{
            HttpServletRequest req = (HttpServletRequest) request;
            HttpServletResponse res = (HttpServletResponse) response;

            String path = req.getRequestURI();
            if(StringUtils.isNotBlank(path)
               && StringUtils.contains(path, ".xhtml")
               && !StringUtils.endsWith(path, "login.xhtml"))
             {
                    if(loginStatus == null
                       || !loginStatus.isLoggedIn())
                     {
                          res.sendRedirect(req.getContextPath() + "/login.xhtml");
                      }
                     else
                      {
                           chain.doFilter(request, response);
                       }
              }
              else
              {
                  chain.doFilter(request, response);
               }
            }catch (Exception ex)
             {
                  log.error(ex);
              }
           }

        .... ....
        }

My css files were referenced in following style:

    <link href="css/styles.css" rel="stylesheet" type="text/css"/>

Everything works well until I change the css reference style to the JSF 2 Resource handler (http://www.mkyong.com/jsf2/resources-library-in-jsf-2-0/). I have copied all my css files under a resources folder and gave library name and version number. So now I reference the css as following:

    <h:outputStylesheet library="default" name="css/styles.css"/>

After the change, the login.xhtml does not render the stylesheet any more. I have a welcome.xhtml page right after login.xhtml page, which has almost identical structure except for the core content, but this page render perfectly fine. I have refreshed the login.xhtml still it does not render. But once I logged in, advance to next page, then come back to login.xhtml, then refresh, the style will get rendered. Also, if I take off the loginCheckFilter, the login.xhtml will rendered the stylesheet. So if anybody ran into the similar situation and know how to resolve it? Thanks!

chaoshangfei
  • 205
  • 4
  • 12

1 Answers1

5
urlPatterns={"/*"}

Your filter is also blocking requests to JSF resources.

You need to rewrite your filter in such way that it allows JSF resource requests.

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {    
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    String loginURL = request.getContextPath() + "/login.xhtml";

    boolean loggedIn = loginStatus != null && loginStatus.isLoggedIn();
    boolean loginRequest = request.getRequestURI().startsWith(loginURL);
    boolean resourceRequest = request.getRequestURI().startsWith(request.getContextPath() + "/faces" + ResourceHandler.RESOURCE_IDENTIFIER);

    if (loggedIn || loginRequest || resourceRequest)) {
        if (!resourceRequest) { // Prevent restricted pages from being cached.
            response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
            response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
            response.setDateHeader("Expires", 0); // Proxies.
        }

        chain.doFilter(request, response);
    } else {
        response.sendRedirect(loginURL);
    }
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    What if I don't have any filters, but I have added a security constrain in web.xml (*.jsf) to restrict access to jsf resources. And, of course, in login page it limits access to css files as well. I'm trying to add another security constrain without specifying auth constrain, but it doesn't work. – whiteErru Mar 09 '16 at 12:43