2013-09-27 40 views
2

我正在學習Spring。執行登錄/註銷功能。 這是我的控制器看起來像:服務器重啓後HttpSession仍然存在

@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/> 
<%} %> 

當我登錄,並重新啓動服務器,我仍然登錄不應該的會話信息會丟失在服務器重新啓動後?如果我重新啓動瀏覽器,它會像它應該那樣工作(即會話信息丟失)。

這是我的春節,安全配置:

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

回答

8

我假設你正在使用Tomcat,它使用Manager組件堅持應用程序生命週期之間的會話。您可以更改the Manager component configuration中的所有設置。

認爲它也取決於你做的改變的種類。 Eclipse的Tomcat服務器插件將決定是否應該刷新序列化的HttpSession

+2

由於控制這!這是一個服務器配置的事情。在context.xml中,我看到了這一行,並進行了更改:取消註釋以禁用跨Tomcat重新啓動的會話持久性: '

6

我猜你使用Tomcat,

從文檔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. 

您可以通過更改屬性設置爲false在您的context.xml

<Manager pathname=""> 
     <saveOnRestart>false</saveOnRestart> 
</Manager> 
相關問題