【问题标题】:HttpSession remains after server restart服务器重启后 HttpSession 仍然存在
【发布时间】:2013-10-03 20:31:00
【问题描述】:

我正在学习 Spring。执行登录/注销功能。 这是我的控制器的样子:

@RequestMapping(value="/successfulLoginAuth", method=RequestMethod.GET)
public ModelAndView postHttpLogin(HttpSession session, Authentication authInfo) 
{

ModelAndView mav = new ModelAndView();
mav.setViewName("redirect:/index.html");
session.setAttribute("authInfo", authInfo);

return mav;

}

使用我已经实现的 dao 服务通过 Spring Security 执行登录。效果很好。

这是 index.jsp 的内容:

<% 
    HttpSession session1 = request.getSession(false);
    Authentication authInfo; 
    if( (session1 != null) && 
        ( (authInfo = (Authentication)session1.getAttribute("authInfo")) != null) 
      )
    {

        out.print(" yo " + authInfo.getName() + " " + authInfo.getAuthorities().iterator().next().getAuthority());
    }
    else
    {
%>    
<a href="${pageContext.request.contextPath}/registration">New? Sign Up!</a><br/>

<a href="${pageContext.request.contextPath}/login">Existing? Sign In!</a><br/>
<%} %>

当我登录并重新启动服务器时,我仍然处于登录状态。服务器重新启动后会话信息不应该丢失吗?如果我重新启动浏览器,它会正常工作(即会话信息丢失)。

这是我的 Spring Security 配置:

<http auto-config="true"  use-expressions="true">
        <intercept-url pattern="/" access="permitAll" />
        <intercept-url pattern="/logout" access="permitAll" />
        <intercept-url pattern="/accessdenied" access="permitAll" />
        <form-login login-page="/login" default-target-url="/successfulLoginAuth" authentication-failure-url="/accessdenied" />
        <logout logout-success-url="/logout" />
    </http>

<authentication-manager>
    <authentication-provider user-service-ref="myUserDetailsService"></authentication-provider>
  </authentication-manager>

【问题讨论】:

    标签: java spring spring-mvc spring-security httpsession


    【解决方案1】:

    我猜你正在使用 Tomcat,

    来自 Docs SaveOnRestart 属性

    Should all sessions be persisted and reloaded when Tomcat is shut down and restarted (or when this application is reloaded)? By default, this attribute is set to true.
    

    您可以通过将 context.xml 中的属性更改为 false 来控制这一点

    <Manager pathname="">
          <saveOnRestart>false</saveOnRestart>
    </Manager> 
    

    【讨论】:

      【解决方案2】:

      我假设您使用的是Tomcat,它使用管理器组件在应用程序生命周期之间保持会话。您可以在the Manager component configuration 中更改所有这些设置。

      认为这也取决于你所做的改变。 Eclipse 的 Tomcat 服务器插件将决定是否刷新序列化的HttpSessions。

      【讨论】:

      • 谢谢!这是服务器配置的事情。在 context.xml 下,我看到了这一行并进行了更改:取消注释以禁用跨 Tomcat 重新启动的会话持久性:&lt;Manager pathname="" /&gt;
      • 如果您想知道为什么您的 Tomcat 会这样做并且从未设置它,请在您的临时目录中的某处查找名为 SESSIONS.ser 的文件 - Tomcat 将此文件保存为正确关闭过程的一部分。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      • 2019-12-22
      • 1970-01-01
      • 2014-03-25
      • 2019-10-22
      相关资源
      最近更新 更多