Spring Security targetUrlParameter不重定向
我正在尝试将用户重定向回他们点击登录链接的页面。 (对于未经过身份validation的用户,页面是只读的,但对于已登录的用户,这些页面是可写的。)如何在用户登录后将用户重定向回他们来自的位置?
我正在通过以下链接向用户发送登录页面: /spring_security_login?redirect=/item5
。 登录后,我希望将用户重定向到/item5
页面。 但是,它们总是被重定向到/
page。
这是我正在使用的配置:
看来targetUrlParameter
没有像预期的那样被拾取。 我正在使用Spring Security 3.1.4
使用SimpleUrlAuthenticationSuccessHandler
时,将应用以下规则:
- 如果alwaysUseDefaultTargetUrl属性设置为true,则defaultTargetUrl属性将用于目标。
- 如果已在请求上设置了与targetUrlParameter的值匹配的参数,则该值将用作目标。 默认情况下,它具有值“spring-security-redirect”。
- 如果设置了useReferer属性,则将使用“Referer”HTTP标头值(如果存在)。
- 作为后备选项,将使用defaultTargetUrl值。
根据您的配置,这应该工作。 我的猜测是,在以登录forms发送POST
请求时,您没有传播referer
。 通常,您应该在登录页面的隐藏字段中写入referer
值,以便将referer
参数传输到spring_security_login
。
将SimpleUrlAuthenticationSuccessHandler更改为SavedRequestAwareAuthenticationSuccessHandler并感到高兴。
使用SavedRequestAwareAuthenticationSuccessHandler
而不是SimpleUrlAuthenticationSuccessHandler
。
即使页面的url是在浏览器上键入的,在这种情况下不会捕获referer
, SavedRequestAwareAuthenticationSuccessHandler
也会使用ExceptionTraslationFilter
捕获的前一个url。
阅读http://docs.spring.io/spring-security/site/docs/3.0.x/reference/core-web-filters.html#form-login-flow-handling和http://docs.spring.io /autorepo/docs/spring-security/3.2.4.RELEASE/apidocs/org/springframework/security/web/authentication/SavedRequestAwareAuthenticationSuccessHandler.html
因为这条线:
删除该行,然后重试。
以下通用解决方案可用于常规登录,Spring Social登录或大多数其他Spring Securityfilter。
在Spring MVC控制器中,当加载只读页面时,如果用户尚未登录,则在会话中保存页面的路径。在XML配置中,设置默认目标URL。 例如:
在Spring MVC控制器中,重定向方法应从会话中读出路径并返回redirect:
。
因此,在用户登录后,它们将被发送到/redirect
页面,这将立即将它们重定向回他们上次访问的页面。
LaurentG已经解释了这一点。 您可以在spring中传递useReferer参数。 适用于SavedRequestAwareAuthenticationSuccessHandler和SimpleUrlAuthenticationSuccessHandler。
这是你修改过的弹簧逻辑: