Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧&x27;s SecurityContextHolder.getContext().getAuthentication()在HTTPS/SSL中使用RedirectView后返回null_Java_Spring_Tomcat_Https_Spring Security - Fatal编程技术网

Java 弹簧&x27;s SecurityContextHolder.getContext().getAuthentication()在HTTPS/SSL中使用RedirectView后返回null

Java 弹簧&x27;s SecurityContextHolder.getContext().getAuthentication()在HTTPS/SSL中使用RedirectView后返回null,java,spring,tomcat,https,spring-security,Java,Spring,Tomcat,Https,Spring Security,我在Tomcat上运行了一个典型的Spring MVC。将系统切换为在HTTPS上运行后(在普通HTTP下一切正常),登录停止工作。原因是Spring的SecurityContextHolder.getContext().getAuthentication()对象在使用RedirectView后变为null 我已经搜索了答案,我找到的唯一一个建议在viewsolverbean设置中将属性redirectHttp10Compatible设置为false。这没有帮助 我还检查了在整个重定向过程中,我

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

我已经搜索了答案,我找到的唯一一个建议在
viewsolver
bean设置中将属性
redirectHttp10Compatible
设置为
false
。这没有帮助

我还检查了在整个重定向过程中,我的会话id保持不变,连接保持安全,也就是说,这不是http和https之间更改的问题(至少据我所知),也不是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_ANONYMOUS, ROLE_USER"/>
    <intercept-url pattern="/signup*" access="ROLE_GUEST, ROLE_ANONYMOUS, 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" />


重定向后,
SecurityContextHolder.getContext().getAuthentication()
变为null是正确的,因为它是线程绑定的。但它应该从会议中重新填充。因此,请尝试跟踪会话中的
SPRING\u SECURITY\u CONTEXT
属性。下面是一些示例代码,以了解您的想法:

HttpSession session = request.getSession(true);
System.out.println(session.getAttribute("SPRING_SECURITY_CONTEXT"));
在Spring安全文档中,有一部分是关于HTTPS/HTTP交换如何会破坏会话的,也许其中有一个问题的提示。

上面的FAQ将检查应用程序中如何处理会话。 我可能会开始研究AuthenticationSuccessHandler实现。(如果您愿意,可以将其张贴到您的问题中。)


有关如何在web应用程序中处理安全上下文的更多详细信息,请参见以下内容:():

是否尝试通过会话访问它?另外,请发布您的配置以帮助进一步调试。我将查看会话和SecurityContext之间的身份验证附件。谢谢您的回复。我应该发布哪些配置文件?Spring安全配置。如果您使用任何自定义类实现,这些可能也会有所帮助。身份验证后,用户将被重定向到/dashboard。正是在这个重定向过程中,身份验证对象变为null。Mmmh…需要一些时间来研究这个问题。也许今晚我有时间。有一件事可能会有帮助:我注意到您仅在仪表板上使用HTTPS,这可能会导致Spring使用的会话出现一些中断,作为第一步,尝试使所有页面都需要HTTPS,并查看问题是否仍然存在。(在旁注中,至少登录也应该是HTTPS,我更喜欢在登录HTTPS后保持所有页面的访问权限,以保持会话数据的加密和更安全)非常感谢!所以(我理解正确吗?)-你是说我应该在会话中将SecurityContext对象设置为属性,然后在重定向后从那里提取它,而不是从SecurityContextHolder提取它?通常Spring安全性应该自己做这件事。根据您自己实现的部分,功能可能会被破坏。尝试找出调试模式下securitycontext发生了什么,并找出该行为源自文档中所述内容的时刻。要缩短此过程:如果您自己填充securityContext,您可能还必须将其附加到会话。这真的很有帮助!不幸的是,我无法投票支持你的答案,这需要我没有的“声誉”:(所以本质上,每次我现在使用redirect时,我都需要从Spring安全上下文对象中提取用户的数据,然后在SecurityContext上重新分配身份验证……Spring安全文档中没有写任何内容,这不是很奇怪吗?另外,我不是在HTTP和HTTPS之间切换,我总是在HTTPS域中。在我的应用程序HttpSess中ion session=request.getSession(true);sysout(session.getAttribute(“SPRING\u SECURITY\u CONTEXT”))为!=null,但SecurityContextHolder.getContext().getAuthentication()为null。