我知道之前曾有人问过这个问题,但是我在这里面临一个特殊的问题。
我使用Spring Security 3.1.3。
我的Web应用程序中有3种可能的登录案例:
请注意,在情况2)中,成功登录后,开箱即用的重定向到受限页面。
这是我的security.xml文件的相关部分:
<!-- Authentication policy for the restricted page --> <http use-expressions="true" auto-config="true" pattern="/restrictedPage/**"> <form-login login-page="/login/restrictedLogin" authentication-failure-handler-ref="authenticationFailureHandler" /> <intercept-url pattern="/**" access="isAuthenticated()" /> </http> <!-- Authentication policy for every page --> <http use-expressions="true" auto-config="true"> <form-login login-page="/login" authentication-failure-handler-ref="authenticationFailureHandler" /> <logout logout-url="/logout" logout-success-url="/" /> </http>
我怀疑“每个页面的身份验证策略”是造成该问题的原因。但是,如果删除它,我将无法登录… j_spring_security_check发送404错误。
编辑:
多亏了Ralph,我才找到了解决方案。这就是问题:我使用了物业
<property name="useReferer" value="true"/>
拉尔夫给我看的 之后,我的情况1出现了问题:通过登录页面登录时,用户停留在同一页面上(而不是像以前那样重定向到主页)。到此阶段为止的代码如下:
<!-- Authentication policy for login page --> <http use-expressions="true" auto-config="true" pattern="/login/**"> <form-login login-page="/login" authentication-success-handler-ref="authenticationSuccessHandlerWithoutReferer" /> </http> <!-- Authentication policy for every page --> <http use-expressions="true" auto-config="true"> <form-login login-page="/login" authentication-failure-handler-ref="authenticationFailureHandler" /> <logout logout-url="/logout" logout-success-url="/" authentication-success-handler-ref="authenticationSuccessHandler"/> </http> <beans:bean id="authenticationSuccessHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"> <!-- After login, return to the last visited page --> <beans:property name="useReferer" value="true" /> </beans:bean> <beans:bean id="authenticationSuccessHandlerWithoutReferer" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"> <!-- After login, stay to the same page --> <beans:property name="useReferer" value="false" /> </beans:bean>
至少从理论上讲,这应该起作用,但事实并非如此。我仍然不知道为什么,所以如果有人对此有答案,我将很乐意创建一个新的主题,让他分享他的解决方案。
同时,我提出了一种解决方法。不是最好的解决方案,但是就像我说的那样,如果有人可以展示一些更好的东西,那么我无所不能。因此,这是登录页面的新身份验证策略:
<http use-expressions="true" auto-config="true" pattern="/login/**" > <intercept-url pattern="/**" access="isAnonymous()" /> <access-denied-handler error-page="/"/> </http>
这里的解决方案非常明显:登录页面仅允许匿名用户使用。连接用户后,错误处理程序会将其重定向到主页。
登录后(将用户重定向到哪个url)发生什么情况,由处理AuthenticationSuccessHandler。
AuthenticationSuccessHandler
该接口(实现它的具体类SavedRequestAwareAuthenticationSuccessHandler)由方法中的AbstractAuthenticationProcessingFilter或子类之一(UsernamePasswordAuthenticationFilter)调用successfulAuthentication。
SavedRequestAwareAuthenticationSuccessHandler)
AbstractAuthenticationProcessingFilter
(UsernamePasswordAuthenticationFilter)
successfulAuthentication。
因此,为了在情况3中进行其他重定向,你必须继承子类SavedRequestAwareAuthenticationSuccessHandler并使其执行你想要的操作。
有时(取决于你的确切用例)足以启用由(的的超类)调用其useReferer标志。AbstractAuthenticationTargetUrlRequestHandlerSimpleUrlAuthenticationSuccessHandlerSavedRequestAwareAuthenticationSuccessHandler
AbstractAuthenticationTargetUrlRequestHandlerSimpleUrlAuthenticationSuccessHandlerSavedRequestAwareAuthenticationSuccessHandler
<bean id="authenticationFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"> <property name="filterProcessesUrl" value="/login/j_spring_security_check" /> <property name="authenticationManager" ref="authenticationManager" /> <property name="authenticationSuccessHandler"> <bean class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"> <property name="useReferer" value="true"/> </bean> </property> <property name="authenticationFailureHandler"> <bean class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"> <property name="defaultFailureUrl" value="/login?login_error=t" /> </bean> </property> </bean>