有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在会话超时时删除自定义cookie

在我的应用程序中,在登录时,我正在创建一个cookie(AAA)。注销时,我可以删除cookie。在自动会话超时时,将用户重定向到登录页面,但无法删除(过期)cookie(AAA)。我在应用程序中使用JBossAS7.1、spring-3.1和spring安全性

以下是我的安全性中的http标记配置。xml

<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并同步会话和cookie之间的时间

选项2-为登录页面url(/login)创建了一个HttpFilter,为登录页面调用Filter并删除cookie

选项1似乎不起作用,因为我可以在会话超时后看到cookie。选项2的问题是,如果登录用户试图从同一浏览器中使用不同的选项卡再次请求登录页面,则会调用筛选器并删除cookie。这很糟糕。因为cookie是进一步通信所必需的

你能帮我删除cookie的正确方法吗

我还想提到在HttPSessionListene#sessionDestroyed方法之前调用过滤器


共 (1) 个答案

  1. # 1 楼答案

    您必须在会话超时时删除cookie
    我们在项目中做了以下工作:

    1. 您正在登录URL上传递一些参数&;根据参数值,您可能正在删除cookie。我们使用不同的URL登录(/auth/login)&;会话超时(/home/sessionExpired)

      In session management标记设置URL

      <session-management session-authentication-strategy-ref="maxSessions"
          invalid-session-url="/home/sessionExpired"/>
      
    2. 在控制器中

      @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";
      }
      

      希望对你有帮助