Java 排除子目录的过滤器映射 url-pattern
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3466897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filter mapping url-pattern that excludes subdirectories
提问by Stefan Rasmusson
Is there any way to make a filtermapping not include subdirectories?
有什么方法可以使过滤器映射不包含子目录?
For example.
例如。
I have .xhtml files in my context root, and I also have a subfolder named "test" with files with the same extension. Is there any may to map a filter to the files in context root and not to the files in the "test" directory?
我的上下文根中有 .xhtml 文件,而且我还有一个名为“test”的子文件夹,其中包含具有相同扩展名的文件。是否可以将过滤器映射到上下文根中的文件而不是“test”目录中的文件?
采纳答案by BalusC
The url-pattern
is indeed restrictive in matching. It only allows exact, prefix or suffix matchnig. Not midst/overall/regex matching. So e.g. /*.xhtml
what you intend to do ain't going to work.
该url-pattern
是匹配确实是限制。它只允许精确、前缀或后缀匹配。不是中间/整体/正则表达式匹配。因此,例如/*.xhtml
,您打算做的事情是行不通的。
If you want to exclude XHTML in the /test
folder only, then your best is really a Filter
listening on an url-pattern
of *.xhtml
which does basically the following job in doFilter()
method:
如果你只想在/test
文件夹中排除 XHTML ,那么你最好的方法就是Filter
监听其中的一个url-pattern
,*.xhtml
基本上在doFilter()
方法中完成以下工作:
// 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.
}
The HttpServletRequest#getServletPath()
basically returns the part of the request URI from the context path on.
所述HttpServletRequest#getServletPath()
基本上返回从上下文路径请求URI的一部分。
You can if necessary configure the value /test
as an <init-param>
of the filter so that you can control the value from inside the web.xml
instead of in the Filter's code.
如有必要,您可以将该值配置/test
为<init-param>
过滤器的一个,以便您可以从web.xml
过滤器代码内部而不是在过滤器代码中控制该值。
回答by Gopi
Some containers (like resin) define an extension to filter mapping definition in web.xml which allows specifying url-regexp
which allows matching the urls based on regular expression. (reference). See if your container has something like this.
一些容器(如树脂)在 web.xml 中定义了过滤器映射定义的扩展,它允许指定url-regexp
哪个允许基于正则表达式匹配 url。(参考)。看看你的容器有没有这样的东西。
回答by mdma
Using what is available for standard servlets/filters, there is no direct way to exclude the subdirectory, since *.ext
style mappings include subdirectories. You could work around this by declaring another filter that specifically handles the /test/*.xhtml
mapping and handles that accordingly.
使用可用于标准 servlet/过滤器的内容,没有直接的方法来排除子目录,因为*.ext
样式映射包括子目录。您可以通过声明另一个专门处理/test/*.xhtml
映射并相应处理的过滤器来解决此问题。
回答by softskin
The solution, basically you need your own Filter class and define excluding URLs as init-param for filter
解决方案,基本上你需要自己的 Filter 类并将排除 URL 定义为过滤器的 init-param
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 the filter, mapping and exclude URLs in web.xml
并在 web.xml 中定义过滤器、映射和排除 URL
<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>
回答by acaruci
You can add a url-mapping value for every file you have in the context root, therefore the filter will apply to those files but none to the files in the test folder (actually it will only apply to those specific files). The following worked for me
您可以为上下文根中的每个文件添加 url-mapping 值,因此过滤器将应用于这些文件,但不适用于测试文件夹中的文件(实际上它仅适用于那些特定文件)。以下对我有用
<url-pattern>/index.faces</url-pattern>