在HTTPS / SSL中使用RedirectView后,Spring的SecurityContextHolder.getContextgetAuthentication返回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上运行了典型的Spring
MVC。将系统切换为在HTTPS上运行(在纯HTTP下一切正常后)后,登录名停止工作。其原因是,Spring的SecurityContextHolder.getContext().getAuthentication()对象变成nullRedirectView使用。

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

我还检查了整个重定向过程中,我的会话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/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://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 id="redefineTargetURL" class="com.groupskeed.common.RedefineTargetURL" />
<beans:bean id="userDetailsService" class="com.groupskeed.security.UserDetailsServiceImpl" />

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...