【问题标题】:Spring Security targetUrlParameter does not redirectSpring Security targetUrlParameter 不重定向
【发布时间】:2013-07-20 16:52:54
【问题描述】:

我正在尝试将用户重定向回他们单击登录链接的页面。 (页面对于未经身份验证的用户是只读的,但对于已登录的用户是可写的。)如何在用户登录后将其重定向回他们来自的地方?

我通过以下链接将用户发送到登录页面:/spring_security_login?redirect=/item5。登录后,我希望用户被重定向到/item5 页面。但是,它们总是被重定向到/ 页面。

这是我正在使用的配置:

<http use-expressions="true">
    <intercept-url pattern="/**" access="permitAll" />
    <form-login authentication-success-handler-ref="simpleUrlAuthenticationSuccessHandler"/>
</http>
<beans:bean id="simpleUrlAuthenticationSuccessHandler"
    class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
    <beans:property name="defaultTargetUrl" value="/"/>
    <beans:property name="targetUrlParameter" value="redirect"/>
</beans:bean>

targetUrlParameter 似乎没有按预期获得。我正在使用 Spring Security 3.1.4

【问题讨论】:

    标签: java spring-security


    【解决方案1】:

    使用SimpleUrlAuthenticationSuccessHandler时应用以下规则:

    • 如果 alwaysUseDefaultTargetUrl 属性设置为 true,则 defaultTargetUrl 属性将用于目标。
    • 如果在请求中设置了与 targetUrlParameter 值匹配的参数,则该值将用作目标。默认情况下,它的值为“spring-security-redirect”。
    • 如果设置了 useReferer 属性,则将使用“Referer”HTTP 标头值(如果存在)。
    • 作为后备选项,将使用 defaultTargetUrl 值。

    根据您的配置,这应该可以。我的猜测是您在表单登录中发送POST 请求时没有传播referer。通常,您应该在登录页面的隐藏字段中写入referer 值,以便将referer 参数传输到spring_security_login

    【讨论】:

    • 我试图让它与 Spring Security 提供的默认登录页面一起工作,所以我没有在登录页面上手动设置或传播任何内容。我没有关注如何设置referer,因为它似乎不能简单地设置为/spring_security_login?referer=somePage
    • 我没有看到除了自己渲染登录页面之外的其他方法(并注意 referer 参数。大多数时候,您将需要这个,因为 Spring 的默认登录页面是丑陋。
    【解决方案2】:

    SimpleUrlAuthenticationSuccessHandler 更改为 SavedRequestAwareAuthenticationSuccessHandler 并感到高兴。

    【讨论】:

    • 对不起,我不知道。
    【解决方案3】:

    因为这条线:

    <beans:property name="defaultTargetUrl" value="/"/>
    

    删除该行并重试。

    【讨论】:

    • 我删除了该行,但它并没有改变行为。即使没有该行,登录页面也始终重定向到/
    【解决方案4】:

    使用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-handlinghttp://docs.spring.io/autorepo/docs/spring-security/3.2.4.RELEASE/apidocs/org/springframework/security/web/authentication/SavedRequestAwareAuthenticationSuccessHandler.html

    【讨论】:

    • 关于推荐人的好信息。我不会依赖它。 Spring 将在 requestCache 中捕获原始请求信息。遇到与 OP 类似的问题的任何人都应该使用调试器并逐步执行 SavedRequestAwareAuthenticationSuccessHandler Spring 代码。很容易了解他们如何重定向到用户的原始预期页面。
    【解决方案5】:

    以下通用解决方案可与常规登录、Spring Social 登录或大多数其他 Spring Security 过滤器一起使用。

    在您的 Spring MVC 控制器中,当加载只读页面时,如果用户尚未登录,则在会话中保存页面的路径。在 XML 配置中,设置默认目标 url。例如:

    在您的 Spring MVC 控制器中,重定向方法应该从会话中读取路径并返回 redirect:&lt;my_saved_page_path&gt;

    因此,在用户登录后,他们将被发送到/redirect 页面,该页面会立即将他们重定向回他们上次访问的页面。

    【讨论】:

      【解决方案6】:

      LaurentG 已经对此进行了解释。您可以在 spring 中传递 useReferer 参数。 SavedRequestAwareAuthenticationSuccessHandler 和 SimpleUrlAuthenticationSuccessHandler 都可以正常工作。

      这是您修改后的弹簧逻辑:

      <http use-expressions="true">
          <intercept-url pattern="/**" access="permitAll" />
          <form-login authentication-success-handler-ref="simpleUrlAuthenticationSuccessHandler"/>
      </http>
      
      <beans:bean id="simpleUrlAuthenticationSuccessHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
           <beans:property name="defaultTargetUrl" value="/"/>
           <beans:property name="targetUrlParameter" value="redirect"/>
           <beans:property name="useReferer" value="true"/>
      </beans:bean>
      

      【讨论】:

      • 有错别字,应该是SimpleUrlAuthenticationSuccessHandler
      猜你喜欢
      • 2015-06-23
      • 2020-08-28
      • 2010-12-18
      • 1970-01-01
      • 2021-09-01
      • 2016-06-15
      • 2015-06-26
      • 2013-03-26
      • 2015-01-12
      相关资源
      最近更新 更多