在我的应用程序中,登录时我正在创建一个 cookie(AAA)。注销时我可以删除 cookie。自动 session 超时时,将用户重定向到登录页面,但无法删除(过期)cookie(AAA)。我在我的应用程序中使用 Jboss AS 7.1、spring-3.1 和 spring-security。
以下是我的 security.xml 中的 http 标记配置
<http auto-config="true" use-expressions="true" entry-point-ref="customLoginUrlAuthenticationEntryPoint" disable-url-rewriting="true">
<request-cache ref="httpSessionRequestCache"/>
<session-management invalid-session-url="/ctx/login?invalid-session=true" session-authentication-error-url="/ctx/login?session-auth-error=true">
<concurrency-control max-sessions="1" expired-url="/ctx/login?expired=true" error-if-maximum-exceeded="true" />
</session-management>
<form-login authentication-success-handler-ref="customAuthenticationSuccessHandler"
authentication-failure-handler-ref="customPageHandler"
login-processing-url="/j_spring_security_check"/>
<custom-filter before="ANONYMOUS_FILTER" ref="anonymousFilter"/>
<custom-filter before="FORM_LOGIN_FILTER" ref="customFilter"/>
<custom-filter before="LOGOUT_FILTER" ref="logoutFilter" />
</http>
我尝试过以下选项
选项 1- 创建一个 HttpFilter 来刷新 cookie 并同步 session 和 cookie 之间的时间。
选项 2- 为登录页面 url(/login) 创建 HttpFilter,为登录页面调用 Filter 并删除 cookie。
选项 1 似乎不起作用,因为我可以在 session 超时后看到 cookie。 选项 2 的问题是,如果登录用户尝试使用不同选项卡从同一浏览器再次请求登录页面,则会调用过滤器并删除 cookie。这很糟糕。因为进一步的通信需要cookie。
您能帮我了解删除 cookie 的正确方法吗?
我还想提一下,过滤器在 HttPSessionListene#sessionDestroyed 方法之前被调用。
最佳答案
您必须在 session 超时时删除 cookie。
我们在项目中做了以下工作:
您正在登录 URL 上传递一些参数,并且根据参数值您可能会删除 cookie。我们使用不同的 URL 进行登录 (/auth/login) 和 session 超时 (/home/sessionExpired)。
在 session 管理标签中设置 URL
<session-management session-authentication-strategy-ref="maxSessions" invalid-session-url="/home/sessionExpired"/>
在 Controller 中,
@RequestMapping(value="/home/sessionExpired") public String handleSessionTimeout(HttpServletRequest request, RedirectAttributes redirectAttributes) { SecurityContextHolder.clearContext(); HttpSession = request.getSession(false); if(session != null) { session.invalidate(); } for(Cookie cookie : request.getCookies()) { cookie.setMaxAge(0); } //err.sessionexpired : make entry in properties file redirectAttributes.addFlashAttribute("message", "err.sessionexpired"); return "redirect:/auth/login"; }
希望对您有帮助。
关于java - 在 session 超时时删除自定义 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23475960/