【发布时间】:2013-06-01 21:47:50
【问题描述】:
我有一个在 Tomcat 上运行的典型 Spring MVC。将系统切换为在 HTTPS 上运行(在纯 HTTP 下一切正常)后,登录停止工作。原因是Spring的SecurityContextHolder.getContext().getAuthentication()对象在使用RedirectView后变成了null。
我已经搜索过答案,我发现的唯一一个建议在viewResolver bean 设置中将属性redirectHttp10Compatible 设置为false。这没有帮助。
我还检查了在整个重定向过程中,我的会话 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_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" />
【问题讨论】:
-
您是否尝试过通过会话访问它?另外请发布您的配置以帮助进一步调试。我会查看 Session 和 SecurityContext 之间的身份验证附件。
-
感谢您的回复。我应该发布哪些配置文件?
-
Spring 安全配置。如果您使用任何自定义类实现,这些也可能会有所帮助。
-
在这里。身份验证后,用户被重定向到 /dashboard。正是在此重定向期间,Authentication 对象变为 null。
-
嗯...需要一些时间来研究一下。也许我今晚能找到一些时间。一件可能有帮助的快速事情:我注意到您仅在仪表板上使用 HTTPS,这可能会对 Spring 使用的会话产生一些刺激,作为第一步,尝试使所有页面都需要 HTTPS,并查看问题是否仍然存在。 (在旁注中,至少登录也应该是 HTTPS,我更喜欢在登录 HTTPS 后保持所有页面访问,以保持会话数据加密并更安全)
标签: java spring tomcat https spring-security