用户登出到上一页打开后重定向URL

时间:2013-07-10 06:35:01

标签: security redirect spring-mvc

我有一个基于Spring MVC的应用程序,当会话过期时,用户会自动注销。再次登录后,我想将用户重定向到他所在的最后一页,我该怎么做。目前,这种情况正在发生

User is on: http://localhost:8080/MYAPP/home/#/access/userManagement

现在会话到期时,

User is on: http://localhost:8080/MYAPP/auth/login#/access/userManagement

再次登录后,

User is on: http://localhost:8080/MYAPP/home/

这是我的配置

<security:form-login
                login-page="/auth/login"
                default-target-url="/home/"
                always-use-default-target="false"
                authentication-failure-url="/auth/login?error=true"/>

我还需要将用户重定向到最后打开的页面???

1 个答案:

答案 0 :(得分:2)

你可以自己编写:

<security:form-login
                login-page="/auth/login"
                authentication-success-handler-ref="customAuthenticationSuccessHandler"
                always-use-default-target="false"
                authentication-failure-url="/auth/login?error=true"/>

<bean id="customAuthenticationSuccessHandler" class=".....MyAuthenticationSuccessHandler">

MyAuthenticationSuccessHandler可以延长SavedRequestAwareAuthenticationSuccessHandler

成功进行身份验证后,它会根据以下情况决定重定向目标:

  • 如果alwaysUseDefaultTargetUrl属性设置为true,则defaultTargetUrl将用于目标。存储在会话中的任何DefaultSavedRequest都将被删除。

  • 如果已在请求中设置targetUrlParameter,则该值将用作目标。任何DefaultSavedRequest都将被删除。

  • 如果在DefaultSavedRequest中找到RequestCache(由ExceptionTranslationFilter设置以在身份验证过程开始之前记录原始目标),将执行重定向原始目的地的网址。 DefaultSavedRequest对象将保持缓存状态,并在收到重定向请求时被接听(请参阅SavedRequestAwareWrapper)。

  • 如果找不到DefaultSavedRequest,它将委托给基类。

所以基本上,你需要替换默认的目标URL行为并用你自己的行为覆盖它。

希望这会有所帮助。欢呼。