如何在java中跳过过滤器链中的过滤器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3738162/
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
How to skip a filter in the filter chain in java
提问by Vanchinathan Chandrasekaran
I have 2 filters in my application. Based on some condition, I want to choose whether to execute the second filter or not. Is there a way to do this?
我的应用程序中有 2 个过滤器。根据某些条件,我想选择是否执行第二个过滤器。有没有办法做到这一点?
I did some googling with no success. I want the request to continue without executing the second filter. Is that possible?
我做了一些谷歌搜索但没有成功。我希望请求在不执行第二个过滤器的情况下继续。那可能吗?
Any help will be appreciated.
任何帮助将不胜感激。
采纳答案by Colin Hebert
You can set an Attribute in your request and check it in your second filter.
您可以在您的请求中设置一个属性并在您的第二个过滤器中检查它。
public class FirstFilter implements Filter {
//...
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setAttribute("executeSecondFilter", true);
//...
if(someReason)
servletRequest.setAttribute("executeSecondFilter", false);
filterChain.doFilter(servletRequest, servletResponse);
}
}
public class SecondFilter implements Filter {
//..
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
if (servletRequest.getAttribute("executeSecondFilter") == null || !((Boolean) servletRequest.getAttribute("executeSecondFilter"))) {
filterChain.doFilter(servletRequest, servletResponse);
}
//...
}
}
You can simplify the code above like this :
您可以像这样简化上面的代码:
public class FirstFilter implements Filter {
//...
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//...
if(someReason)
servletRequest.setAttribute("executeSecondFilter", false);
filterChain.doFilter(servletRequest, servletResponse);
}
}
public class SecondFilter implements Filter {
//..
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
if (servletRequest.getAttribute("executeSecondFilter") != null) {
filterChain.doFilter(servletRequest, servletResponse);
}
//...
}
}
This way you just check the presence of the attribute "executeSecondFilter"
这样您只需检查属性“executeSecondFilter”的存在
回答by BalusC
In addition to Colin's answer, there's another way: just don't call FilterChain#doFilter()
, but RequestDispatcher#forward()
.
除了科林的回答,还有另一种方式:不要打电话FilterChain#doFilter()
,但是RequestDispatcher#forward()
。
if (condition) {
request.getRequestDispatcher(((HttpServletRequest) request).getServletPath()).forward(request, response);
} else {
chain.doFilter(request, response);
}
But this will skip allfilters from current on, expect of the ones which are listening on <dispatcher>FORWARD</dispatcher>
.
但这将从当前开始跳过所有过滤器,期待正在侦听的过滤器<dispatcher>FORWARD</dispatcher>
。