In a web.xml url-pattern matcher is there a way to exclude URLs?

前端 未结 4 870
迷失自我
迷失自我 2020-12-13 19:43

I wrote a filter that needs to be invoked every time a url on my site is accessed EXCEPT the CSS, JS, and IMAGE files. So in my definition I\'d like to have something like

相关标签:
4条回答
  • 2020-12-13 20:25

    I used the security-constraint to access control. See the code:

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Unsecured resources</web-resource-name>
            <url-pattern>/resources/*</url-pattern>
            <url-pattern>/javax.faces.resource/*</url-pattern>
        </web-resource-collection>
    </security-constraint>
    

    I follow this tutorial.

    0 讨论(0)
  • 2020-12-13 20:33

    The URL pattern mapping does not support exclusions. This is a limitation of the Servlet specification. You can try the manual workaround posted by Mr.J4mes.

    0 讨论(0)
  • 2020-12-13 20:37

    I think you can try this one:

    @WebFilter(filterName = "myFilter", urlPatterns = {"*.xhtml"})
    public class MyFilter implements Filter {
    
       @Override
       public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {
          String path = ((HttpServletRequest) request).getServletPath();
    
          if (excludeFromFilter(path)) chain.doFilter(request, response);
          else // do something
       }
    
       private boolean excludeFromFilter(String path) {
          if (path.startsWith("/javax.faces.resource")) return true; // add more page to exclude here
          else return false;
       }
    }
    
    0 讨论(0)
  • 2020-12-13 20:38

    Probably you could declare another "blank" filter for css, js etc, and put it before others filter mapping.

    0 讨论(0)
提交回复
热议问题