在web.xml中,url-pattern匹配器有没有办法排除URL? [英] In a web.xml url-pattern matcher is there a way to exclude URLs?

查看:2202
本文介绍了在web.xml中,url-pattern匹配器有没有办法排除URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

Is there anyway to do this? The only documentation I can find has only /*

更新:

I结果使用类似于Mr.J4mes提供的答案:

I ended up using something similar to an answer provided by 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());
   }


推荐答案

我想你可以试试这个一个:

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

这篇关于在web.xml中,url-pattern匹配器有没有办法排除URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆