Spring Security 3 SavedRequestAwareAuthenticationSuccessHandler未保存原始请求

时间:2011-12-07 16:22:46

标签: spring-security

我正在尝试配置spring security 3,以便当用户被迫再次登录时(例如,当会话超时时),用户将在授权过程开始之前收回他们所在页面。< / p>

我使用的是SavedRequestAwareAuthenticationSuccessHandler,但原始请求似乎没有保存在缓存中。

以下是我的安全配置。

<security:http auto-config="false" 
               use-expressions="true"
               access-denied-page="/views/auth/login?error=true"
               entry-point-ref="authenticationEntryPoint"  >
    <security:intercept-url pattern="/*" access="hasRole('ROLE_USER')" />
    <security:intercept-url pattern="/views/*" access="hasRole('ROLE_USER')" />
    <security:intercept-url pattern="/data/*" access="hasRole('ROLE_USER')" />  
    <security:intercept-url pattern="/auth/*" access="permitAll" />
    <security:logout invalidate-session="true" logout-success-url="/views/auth/login" logout-url="/views/auth/logout" />
        <security:session-management invalid-session-url="/views/auth/login" >
        <security:concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
    </security:session-management>
    <security:custom-filter ref="authenticationFilter" position="FORM_LOGIN_FILTER"/>
</security:http>

<security:authentication-manager />

<bean id="authenticationFilter" class="com.security.web.filter.UsernamePasswordAuthenticationFilter">             
    <property name="allowSessionCreation" value="true" />
    <property name="authenticationManager" ref="authenticationManager"/>
    <property name="authenticationFailureHandler" ref="aAuthenticationFailureHandler" />
    <property name="authenticationSuccessHandler" ref="authenticationSuccessHandler" />
</bean>

<bean id="authenticationManager" class="com.security.web.manager.AuthenticationManager" />

<bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint" >
    <property name="loginFormUrl" value="/views/auth/login"/>
</bean>

<bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
    <property name="defaultFailureUrl" value="/views/auth/login?error=true"/>
 </bean>

<bean id="authenticationSuccessHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
    <property name="defaultTargetUrl" value="/views"/>
</bean>

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:3)

我知道这个问题已经过时了,但我想记录下我为其他人找到的内容。

简答

问题是,如果您在&lt; session-management&gt; 标记上使用 invalid-session-url ,那么原始请求将不会保存在高速缓存中。

说明

这在春季源论坛上讨论过: http://forum.springsource.org/showthread.php?89352-requestCache-null-when-using-session-management-gt-invalid-session-url

在上述论坛中,安全团队的高级成员Luke Taylor说:

  

invalid-session-url表示上一个会话已过期,用户应该重新开始。与用户登录后用于恢复请求的RequestCache没有任何关联。

  

当会话过期时,您无法知道继续使用请求的URL是安全的,因为某些必需的状态可能与会话一起丢失。在你的特定情况下可能没问题,但一般来说这不是一个安全的假设。


答案 1 :(得分:0)

你可能会错过这个:

<bean id="requestCacheAwareFilter"
      class="org.springframework.security.web.savedrequest.RequestCacheAwareFilter">
    <constructor-arg ref="requestCache"/>
</bean>

<bean id="requestCache" class="org.springframework.security.web.savedrequest.HttpSessionRequestCache"/>

您还需要将requestCache添加到authenticationSuccessHandler

<property name="requestCache" ref="requestCache"/>