gpt4 book ai didi

google-chrome - 在 Apache Tomcat 中部署的 Webapp 登录失败

转载 作者:行者123 更新时间:2023-11-28 22:17:04 25 4
gpt4 key购买 nike

我在 Apache Tomcat 中部署了一个简单的网络应用程序,它有一个登录页面、一个上传表单和一个注销按钮。

提交登录表单后,我将检查凭据并将其重定向到上传页面(如果登录成功),否则我会将请求重定向到登录页面本身。

我还有一个过滤器 (javax.servlet.Filter),用于验证每个请求是否来自登录用户。

昨天一切正常,但今天即使使用有效的用户名/密码,我也会被重定向到登录页面。这只发生在 Chrome 中。

如果我使用 Firefox 或在 chrome 中打开一个incognito 窗口,流程运行得非常好。

当我调试时,我看到在成功登录时进行重定向时 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 浏览器会出现这种情况??我是否正确处理了事情。

谢谢

最佳答案

我尝试删除 Chrome 中的 cookie,我的登录链没有问题。

但是,我仍在努力弄清楚到底发生了什么(使用 chrome)以及清除 cookie 对我有何帮助。

已编辑:

根据 Shadab Faiz 的 comment下面的答案似乎是准确的,因此我接受它:

What happens is that sometimes browser may store previous request data. So when you input wrong credentials, it stored that request. So from next one onwards, whenever you inputted correct info, the previous request with wrong info was sent.

谢谢

关于google-chrome - 在 Apache Tomcat 中部署的 Webapp 登录失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42731078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com