Spring security 成功登录后会话是否丢失?
我正在使用SpringSecurity 3.0.2。所有的应用程序页面都是安全的, 所以你必须通过身份验证才能看到它们 我使用的是https协议 我有一个奇怪的问题:成功登录并转到请求页面后,当我试图打开应用程序中其他页面的任何链接时,会话无效或丢失,用户变得匿名并重定向到登录页面。我是通过调试得到的:Spring security 成功登录后会话是否丢失?,spring-security,Spring Security,我正在使用SpringSecurity 3.0.2。所有的应用程序页面都是安全的, 所以你必须通过身份验证才能看到它们 我使用的是https协议 我有一个奇怪的问题:成功登录并转到请求页面后,当我试图打开应用程序中其他页面的任何链接时,会话无效或丢失,用户变得匿名并重定向到登录页面。我是通过调试得到的: No HttpSession currently exists No SecurityContext was available from the HttpSession: null. A ne
No HttpSession currently exists
No SecurityContext was available from the HttpSession: null. A new one will be created.
在多次查看代码后,代码中的任何内容都不会使会话无效。有什么想法吗?为什么会发生这种情况?应用程序安全,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>
可能是cookie域或cookie路径问题。您的https登录页是否位于同一路径/域上?我也遇到了同样的问题。我从Jboss 7.0迁移到Wildfly 8.0,在Jboss 7.0中行为正常(登录成功并重定向到索引页),但在Wilfly中登录成功,重定向到索引页,但后来会话丢失,Spring Security再次重定向到登录页
我在web navigator(chrome)中看到了cookies,在同一个域(127.0.0.1)中有两个cookie JSESSIONID,它们的值不同。我删除了所有Cookie并再次执行了日志记录过程,一切正常。谢谢您的回答。从Glassfish迁移到WildFly后,我一直在尝试登录。每次我使用SpringSecurity成功登录时,会话都会丢失,并创建一个匿名用户,导致重新加载登录页面。我清除了所有本地主机浏览器cookie,终于可以登录了@谢谢你的评论。我也遇到了同样的问题,清除浏览器cookies为我解决了这个问题。虽然答案总是令人满意,但提供一些关于您的代码如何解决手头问题的信息确实很有帮助。请提供一些关于你答案的上下文,并参阅以获取有关如何写出好答案的信息。