在HTTPS /SSL中使用RedirectView后,Spring的SecurityContextHolder.getContext()getAuthentication()返回null

【字号: 日期:2024-03-30浏览:48作者:雯心
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解决在HTTPS /SSL中使用RedirectView后,Spring的SecurityContextHolder.getContext()getAuthentication()返回null?

该SecurityContextHolder.getContext().getAuthentication()既然是threadbound重定向后成为空是正确的。但是应该从会话中重新填充它。因此,请尝试跟踪SPRING_Security_CONTEXT会话中的属性。以下是一些示例代码,可以帮助您理解:

HttpSession session = request.getSession(true);System.out.println(session.getAttribute('SPRING_Security_CONTEXT'));

在Spring Security文档中,有一个关于HTTPS / HTTP切换如何使会话搞糟的部分,也许这暗示了您的问题。http://static.springsource.org/spring-security/site/faq.html#d0e223

上面的常见问题解答可以检查您的应用程序中如何处理会话。我可能会开始研究AuthenticationSuccessHandler实现。(如果愿意,可以将其插入您的问题中。)

有关如何在Web应用程序中处理安全上下文的更多详细信息,请参见以下内容:(第5.4节):http ://static.springsource.org/spring-security/site/docs/3.0.x/reference/technical-overview。html

解决方法

我在Tomcat上运行了典型的SpringMVC。将系统切换为在HTTPS上运行(在纯HTTP下一切正常后)后,登录名停止工作。其原因是,Spring的SecurityContextHolder.getContext().getAuthentication()对象变成null后RedirectView使用。

我已经搜索的答案,唯一一个我发现建议设置属性redirectHttp10Compatible,以false在viewResolverbean的设置。这没有帮助。

我还检查了整个重定向过程中,我的会话ID保持不变,并且连接保持安全,即,http和https之间的更改(反之亦然)不是问题(至少据我所知)。

可能是什么问题呢?

<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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/securityhttp://www.springframework.org/schema/security/spring-security-3.1.xsd'> <http auto-config='true'> <intercept-url pattern='/**' requires-channel='https' /> <intercept-url pattern='/index*' access='ROLE_USER'/> <intercept-url pattern='/dashboard*' access='ROLE_USER' requires-channel='https'/> <intercept-url pattern='/login*' access='ROLE_GUEST,ROLE_ANONYMOUS,ROLE_USER'/> <intercept-url pattern='/signin*' access='ROLE_GUEST,ROLE_USER'/> <intercept-url pattern='/signup*' access='ROLE_GUEST,ROLE_USER'/> <form-login login-page='/home' default-target-url='/home' authentication-failure-url='/home?authentication_error=true'authentication-success-handler-ref='redefineTargetURL' /> <anonymous username='guest' granted-authority='ROLE_GUEST' key='anonymousKey'/> <logout invalidate-session='true' logout-success-url='/logout?message=Logout Successful' /> </http><authentication-manager alias='authenticationManager'> <authentication-provider user-service-ref='userDetailsService' /></authentication-manager><beans:bean /><beans:bean />

相关文章: