【发布时间】:2012-10-18 08:39:57
【问题描述】:
我想实现一个过滤器来进行身份验证,但不知何故它陷入了无限循环......任何想法都值得赞赏。
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
doBeforeProcessing(request, response);
Throwable problem = null;
HttpSession session = httpRequest.getSession(true);
if(session.getAttribute("userName")!=null&&session.getAttribute("userName")!=(""))
{
try {
chain.doFilter(request, response);
} catch (Throwable t) {
// If an exception is thrown somewhere down the filter chain,
// we still want to execute our after processing, and then
// rethrow the problem after that.
problem = t;
t.printStackTrace();
}
}else{
httpResponse.sendRedirect("login.jsp");
return;
}
调试模式下的这段代码只会无限次运行,基本上我想在用户未登录时将用户重定向到 login.jsp。 任何答案表示赞赏。
【问题讨论】:
-
这个过滤器是否也在
login.jsp页面上运行? -
是的,很遗憾。无论如何,我想出了一个解决方案:“if (uri.indexOf("login.jsp")>-1) { chain.doFilter(request, response); // 继续 chain.return; } "
-
或者有没有更优雅的方法可以将 login.jsp 从过滤中排除?我认为 web.xml 中有一些内容,但据我所知,它不支持此
标签或类似此功能的东西:( -
那么这绝对是你的循环的原因,你已经找到了问题的解决方案......不幸的是,过滤器 url-mapping 没有排除语法
标签: java authentication servlets servlet-filters infinite-loop