Filter mapping url-pattern, which excludes subdirectories

Is there any way to make filtering not include subdirectories?

For example.

I have .xhtml files in my root context, and I also have a subfolder named "test" with files with the same extension. Is it possible to map the filter to files in the context root, and not to files in the "test" directory?

+6
java servlets servlet-filters
source share
5 answers

url-pattern really restrictive when matching. It allows only exact, prefix or suffix matchin. The match is not average / general / regular. So, for example, /*.xhtml what you intend to do will not work.

If you want to exclude XHTML only in the /test folder, the best thing is Filter listening to url-pattern *.xhtml , which performs basically the following task in the doFilter() method:

 // First cast ServletRequest to HttpServletRequest. HttpServletRequest hsr = (HttpServletRequest) request; // Check if requested resource is not in /test folder. if (!hsr.getServletPath().startsWith("/test/")) { // Not in /test folder. Do your thing here. } 

HttpServletRequest#getServletPath() basically returns part of the request URI from the context path.

If necessary, you can configure the /test value as a <init-param> filter so that you can control the value from web.xml instead of the filter code.

+17
source share

Solution, basically you need your own filter class and define the URL exception as init-param for the filter

 package test; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginValidationFilter implements Filter { private String loginPage = ""; private List<String> excludedURLs; public void init(FilterConfig filterConfig) throws ServletException { this.loginPage = filterConfig.getInitParameter("loginPage"); String[] excluded = filterConfig.getInitParameter("excludedURLs").split(";"); excludedURLs = new ArrayList<String>(); for (int i = 0; i < excluded.length; i++) { excludedURLs.add(excluded[i]); } } public void destroy() { this.loginPage = ""; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest httpRequest = request instanceof HttpServletRequest ? (HttpServletRequest) request : null; HttpServletResponse httpResponse = response instanceof HttpServletResponse ? (HttpServletResponse) response : null; if (httpRequest == null || httpResponse == null) { filterChain.doFilter(request, response); return; } boolean isExcludedURL = false; for (int i = 0; i < excludedURLs.size(); i++) { if (httpRequest.getRequestURL().indexOf(excludedURLs.get(i)) > -1) { isExcludedURL = true; break; } } if (isExcludedURL) { filterChain.doFilter(request, response); } else { if (UserUtil.validateUserLogin(httpRequest)) { filterChain.doFilter(request, response); } else { httpResponse.sendRedirect(httpRequest.getContextPath() + loginPage); } } } } 

And define filters, matching, and exclude URLs in web.xml

 <filter> <filter-name>LoginValidationFilter</filter-name> <filter-class>test.LoginValidationFilter</filter-class> <init-param> <param-name>loginPage</param-name> <param-value>/login.jsp</param-value> </init-param> <init-param> <param-name>excludedURLs</param-name> <param-value>/admin/;/assets/;/content/;/css/;/js/;/login.jsp;/login.cmd;/logout.cmd;forgot_password.jsp;pageName=</param-value> </init-param> </filter> <filter-mapping> <filter-name>LoginValidationFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 
+3
source share

Using what is available for standard servlets / filters, there is no direct way to exclude a subdirectory, since *.ext style mappings include subdirectories. You can get around this by declaring another filter that specifically handles the /test/*.xhtml display and processes them accordingly.

0
source share

You can add a URL value for each file that you have in the context root, so the filter will be applied to these files, but not to the files in the test folder (in fact, this applies only to these specific files). The following worked for me

 <url-pattern>/index.faces</url-pattern> 
0
source share

Some containers (like resin) define an extension to define the filter display in web.xml, which allows you to specify url-regexp , which allows you to match URLs based on a regular expression. ( link ). See if there is something like this in your container.

-one
source share

All Articles