Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

UPDATE:

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());    } 
like image 769
kasdega Avatar asked Dec 28 '11 17:12

kasdega


People also ask

How do I exclude a URL from filter-mapping?

The solution for excluding URLs from a third-party filter is to wrap it with a new custom filter which just adds the exclude functionality and delegates the filter logic to the wrapped class. // Forward the request to the next filter or servlet in the chain.

How do I filter in Web xml?

Filters are deployed in the deployment descriptor file web. xml and then map to either servlet names or URL patterns in your application's deployment descriptor. When the web container starts up your web application, it creates an instance of each filter that you have declared in the deployment descriptor.

What is the use of URL pattern in Web xml?

Servlets and URL Paths xml defines mappings between URL paths and the servlets that handle requests with those paths. The web server uses this configuration to identify the servlet to handle a given request and call the class method that corresponds to the request method (e.g. the doGet() method for HTTP GET requests).


2 Answers

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;    } } 
like image 117
Mr.J4mes Avatar answered Sep 27 '22 17:09

Mr.J4mes


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.

like image 30
Perception Avatar answered Sep 27 '22 19:09

Perception