6

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

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

}

登录是通过 Spring Security 使用我实现的 dao 服务执行的。这很好用。

这是 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>
4

2 回答 2

11

我假设您正在使用Tomcat,它使用 Manager 组件在应用程序生命周期之间保持会话。您可以在Manager 组件配置中更改所有这些设置。

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

于 2013-09-27T13:52:56.223 回答
8

我猜你正在使用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> 
于 2013-09-27T14:06:00.023 回答