2010-05-05 123 views
5

我使用spring security 3.0.2。所有應用程序頁面都是安全的, 因此您必須通過身份驗證才能看到它們。登錄成功後會話丟失?

我正在使用https協議。

我有一個奇怪的問題:成功登錄並進入請求頁面後,當我嘗試打開應用程序中其他頁面的任何鏈接時,會話失效或丟失,用戶變爲匿名並重定向到登錄頁面。我得到這個從調試:

No HttpSession currently exists 
No SecurityContext was available from the HttpSession: null. A new one will be created. 

檢查代碼多次後,沒有在代碼無效的會話。有任何想法嗎?爲什麼可能會發生這樣的事情?

回答

1

應用程序的安全性,XML:

<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
         http://www.springframework.org/schema/security 
         http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 

    <global-method-security pre-post-annotations="enabled"> 

    </global-method-security> 
    <http use-expressions="true" disable-url-rewriting="true"> 
     <remember-me token-repository-ref="tokenRepository" 
     token-validity-seconds="1209600"/> 
     <access-denied-handler error-page="/error.jsp"/> 

     <intercept-url pattern="/" access="permitAll" /> 
     <intercept-url pattern="/**/images/**" filters="none" /> 
     <intercept-url pattern="/**/files/**" filters="none" /> 
     <intercept-url pattern="/images/**" filters="none" /> 
     <intercept-url pattern="/scripts/**" filters="none" /> 
     <intercept-url pattern="/styles/**" filters="none" /> 
     <intercept-url pattern="/p/login" filters="none" /> 
     <intercept-url pattern="/p/register" filters="none" /> 
     <intercept-url pattern="/p/forgotPassword" filters="none" /> 
     <intercept-url pattern="/p/changePassword" filters="none" /> 
     <intercept-url pattern="/p/**" access="isAuthenticated()" /> 
     <custom-filter position="LAST" ref="rememberMeFilter"/>  
     <form-login     
      login-processing-url="/j_spring_security_check"   
      login-page="/p/login" 
      authentication-failure-url="/p/login?login_error=1"  
      authentication-success-handler-ref="myAuthenticationHandler"    
     /> 

     <logout /> 
    </http> 

    <beans:bean id="myAuthenticationHandler" class="com.myAuthenticationHandler" /> 
    <beans:bean id="rememberMeFilter" class="com.rememberMeFilter" /> 

    <beans:bean id="tokenRepository" class="org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl"> 
    <beans:property name="dataSource" ref="dataSource"/> 
    </beans:bean> 


    <authentication-manager alias="authenticationManager"> 
    <authentication-provider> 

      <password-encoder hash="md5"/>   
      <jdbc-user-service data-source-ref="dataSource" 
      users-by-username-query="SELECT u.username,u.password,u.enabled 
           FROM Users u where u.username=lower(?)"  
     authorities-by-username-query="SELECT a.username,a.authority  
           FROM Users u, authorities a 
           WHERE u.username=a.username 
           and u.username=lower(?) and enabled=1"/> 

     </authentication-provider> 
    </authentication-manager> 

    </beans:beans> 
+0

雖然答案總是值得讚賞的,但它確實有助於提供有關代碼如何解決手頭問題的信息。請提供一些關於您的答案的背景信息,並參閱[幫助文章](http://stackoverflow.com/help/how-to-answer)以獲取有關如何編寫出色答案的信息。 – 2017-02-14 01:05:53

+0

我也面臨類似的問題,你如何解決? – 2017-02-14 06:07:55

2

可能是一個Cookie域或Cookie路徑的問題。你是https登錄頁面在相同的路徑/域?

+2

謝謝你的回答。從Glassfish遷移到WildFly後,我一直在嘗試登錄。每次我使用spring security成功登錄時,會話都會丟失,並且創建匿名用戶,導致登錄頁面重新加載。我清除了所有localhost瀏覽器cookie,並最終可以登錄! – 2014-02-06 09:08:27

+0

@ChrisRitchie感謝您的評論。我遇到了同樣的問題,並清除瀏覽器cookie爲我解決了這個問題。 – 2014-07-28 19:52:16

2

我有同樣的問題。我從Jboss 7.0遷移到了Wildfly 8.0,在Jboss 7.0中,行爲是OK的(登錄成功並重定向到索引頁面),但是在Wilfly登錄成功後,重定向到索引頁面,但後來會話丟失,Spring Security重定向到登錄頁面。

我在web導航器(chrome)中看到了cookie,並且在同一個域(127.0.0.1)中出現了兩個具有不同值的cookie JSESSIONID。我刪除了所有的cookies並重新執行了日誌記錄程序,這沒關係。