繁体   English   中英

在web.xml中,url-pattern matcher有没有办法排除URL?

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

我编写了一个过滤器,每次访问我的站点上的url时都需要调用它,除了CSS,JS和IMAGE文件。 所以在我的定义中,我希望有类似的东西:

<filter-mapping>
   <filter-name>myAuthorizationFilter</filter-name>
   <url-pattern>NOT /css && NOT /js && NOT /images</url-pattern>
</filter-mapping>

反正有没有这样做? 我能找到的唯一文件只有/ *

更新:

我最终使用类似于Mr.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;
   }
}

URL模式映射不支持排除。 这是Servlet规范的限制。 您可以尝试Mr.J4mes发布的手动解决方法。

可能你可以为cssjs等声明另一个“空白”过滤器,并把它放在其他过滤器映射之前。

我使用security-constraint来访问控制。 看代码:

<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>

我按照教程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM