成功登录后会话丢失?

时间:2010-05-05 22:05:48

标签: spring-security

我正在使用spring security 3.0.2。所有应用程序页面都是安全的, 所以你必须通过身份验证才能看到它们。

我正在使用https协议。

我有一个奇怪的问题:成功登录并转到请求的页面后,当我尝试打开应用程序中其他页面的任何链接时,会话失效或丢失,用户变为匿名并重定向到登录页面。我从调试中得到了这个:

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

多次查看代码后,代码中没有任何内容使会话无效。有任何想法吗?为什么会发生这种情况?

3 个答案:

答案 0 :(得分:2)

可能是Cookie域或Cookie路径问题。您是在同一路径/域上的https登录页面吗?

答案 1 :(得分:2)

我遇到了同样的问题。我从Jboss 7.0迁移到Wildfly 8.0,在Jboss 7.0中行为没问题(登录成功并重定向到索引页面),但在Wilfly登录成功,重定向到索引页但后来会话丢失,Spring Security重定向登录再次翻页。

我在网页导航器(Chrome)中看到了Cookie,并且在同一个域(127.0.0.1)中有两个Cookie JSESSIONID具有不同的值。我删除了所有cookie并再次执行了日志记录程序,这没问题。

答案 2 :(得分: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>