我在Apache Tomcat中部署了一个简单的Web应用程序,它有一个登录页面,一个上传表单和一个注销按钮。
当提交登录表单时,我正在检查凭据并在登录成功时将其重定向到上载页面,如果不是,我将请求重定向到登录页面本身。
我还有一个过滤器 (javax.servlet.Filter),用于验证每个请求是否来自登录用户。
昨天一切正常,但今天即使有了有效的用户名/密码,我也会被重定向到登录页面。这只发生在 Chrome 中。
如果我在 chrome 中使用 Firefox 或打开隐身窗口,则流量可以正常运行。
当我调试时,我看到 request.session 在成功登录时进行重定向时返回null。
我的LoginServlet:
if (success) {
........
...........
HttpSession session = request.getSession(true);
session.setAttribute(WebAppConstants.OAUTH_TOKEN_SESSION_ATTRIB, accessToken);
session.setAttribute(WebAppConstants.USER_SESSION_ATTRIB, username);
session.setAttribute(WebAppConstants.IS_LOGGED_IN_SESSION_ATTRIB, true);
session.setMaxInactiveInterval(30 * 60);
Cookie usernameCookie = new Cookie(WebAppConstants.USER_SESSION_ATTRIB, username);
usernameCookie.setMaxAge(30 * 60);
response.addCookie(usernameCookie);
response.sendRedirect(WebAppConstants.UPLOADER_JSP);
} else {
response.sendRedirect(WebAppConstants.INVALID_LOGIN_JSP);
}
我的LoginCheckFilter:
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpSession session = request.getSession(false);
String loginURI = request.getContextPath() + "/login.html";
String uri = request.getRequestURI();
this.context.log("Requested Resource::" + uri);
if (session == null && !(uri.endsWith("html") || uri.endsWith("login"))) {
this.context.log("Unauthorized access request");
response.sendRedirect(loginURI);
} else {
filterChain.doFilter(request, response); // Logged-in user found, so just continue request.
}
}
为什么这会在 Chrome 浏览器中发生?我是否正确处理过。
谢谢
答案 0 :(得分:0)
我尝试删除 Chrome 中的Cookie,我的登录链工作没有问题。
但是,我仍然试图清楚地了解实际发生的事情(使用chrome)以及清除Cookie对我有何帮助。
<强>编辑:强>
根据Shadab Faiz的comment,上面的答案似乎是准确的,因此我接受了:
有时候浏览器可能会存储以前的请求数据。因此,当您输入错误的凭据时,它会存储该请求。因此,从下一个开始,无论何时输入正确的信息,都会发送错误信息的先前请求。
谢谢