spring的securitycontextholder.getcontext().getauthentication()在https/ssl中使用redirectview后返回null

vawmfj5a  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(548)

我在tomcat上运行了一个典型的spring mvc。将系统切换为在https上运行后(在普通http下一切正常),登录停止工作。原因是Spring SecurityContextHolder.getContext().getAuthentication() 对象变成 null 之后 RedirectView 使用。
我已经搜索了答案,我找到的唯一一个建议设置属性 redirectHttp10CompatiblefalseviewResolver bean设置。这没有帮助。
我还检查了在整个重定向过程中,我的会话id保持不变,连接保持安全,也就是说,这不是http和https之间更改的问题(至少据我所知),也不是http和https之间更改的问题。
有什么问题吗?

  1. <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"
  2. xsi:schemaLocation="http://www.springframework.org/schema/beans
  3. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  4. http://www.springframework.org/schema/security
  5. http://www.springframework.org/schema/security/spring-security-3.1.xsd">
  6. <http auto-config="true">
  7. <intercept-url pattern="/**" requires-channel="https" />
  8. <intercept-url pattern="/index*" access="ROLE_USER"/>
  9. <intercept-url pattern="/dashboard*" access="ROLE_USER" requires-channel="https"/>
  10. <intercept-url pattern="/login*" access="ROLE_GUEST, ROLE_ANONYMOUS, ROLE_USER"/>
  11. <intercept-url pattern="/signin*" access="ROLE_GUEST, ROLE_ANONYMOUS, ROLE_USER"/>
  12. <intercept-url pattern="/signup*" access="ROLE_GUEST, ROLE_ANONYMOUS, ROLE_USER"/>
  13. <form-login login-page="/home"
  14. default-target-url="/home"
  15. authentication-failure-url="/home?authentication_error=true"
  16. authentication-success-handler-ref="redefineTargetURL"
  17. />
  18. <anonymous username="guest" granted-authority="ROLE_GUEST" key="anonymousKey"/>
  19. <logout invalidate-session="true" logout-success-url="/logout?message=Logout Successful" />
  20. </http>
  21. <authentication-manager alias="authenticationManager">
  22. <authentication-provider user-service-ref="userDetailsService" />
  23. </authentication-manager>
  24. <beans:bean id="redefineTargetURL" class="com.groupskeed.common.RedefineTargetURL" />
  25. <beans:bean id="userDetailsService" class="com.groupskeed.security.UserDetailsServiceImpl" />
xj3cbfub

xj3cbfub1#

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

  1. HttpSession session = request.getSession(true);
  2. System.out.println(session.getAttribute("SPRING_SECURITY_CONTEXT"));

在spring安全文档中,有一部分是关于https/http交换如何会破坏会话的,也许其中有一个问题的提示。http://static.springsource.org/spring-security/site/faq.html#d0e223
上面的faq将检查应用程序中如何处理会话。我可能会开始研究authenticationsuccesshandler实现(如果您愿意,可以将其发布到您的问题中。)
有关如何在web应用程序中处理安全上下文的更多详细信息,请参见以下内容:(第5.4节web应用程序中的身份验证):http://static.springsource.org/spring-security/site/docs/3.0.x/reference/technical-overview.html

相关问题