我编写了一个过滤器,除CSS,JS和IMAGE文件外,每次访问我网站上的URL都需要调用该过滤器。因此,在我的定义中,我希望有以下内容:
<filter-mapping> <filter-name>myAuthorizationFilter</filter-name> <url-pattern>NOT /css && NOT /js && NOT /images</url-pattern> </filter-mapping>
反正有这样做吗?我可以找到的唯一文档只有/ *
更新:
我最终使用了类似于J4mes先生提供的答案的方法:
private static Pattern excludeUrls = Pattern.compile("^.*/(css|js|images)/.*$", Pattern.CASE_INSENSITIVE); private boolean isWorthyRequest(HttpServletRequest request) { String url = request.getRequestURI().toString(); Matcher m = excludeUrls.matcher(url); return (!m.matches()); }
我认为您可以尝试以下一种方法:
@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; } }