Spring Security登录成功后重定向到上一页

2023-11-29

我知道这个问题之前已经被问过,但是我在这里面临一个特定的问题。

我使用弹簧安全3.1.3。

我的 Web 应用程序中有 3 种可能的登录情况:

  1. 通过登录页面登录:好的。
  2. 通过受限页面登录:也可以。
  3. 通过非限制页面登录:不行...每个人都可以访问“产品”页面,并且用户如果登录就可以发表评论。因此,同一页面中包含登录表单,以允许用户进行连接。

情况 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 错误。


EDIT:

感谢拉尔夫,我找到了解决方案。所以事情是这样的:我使用了该属性

<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.

这个接口(实现它的具体类是SavedRequestAwareAuthenticationSuccessHandler) 被调用AbstractAuthenticationProcessingFilter或其子类之一,例如(UsernamePasswordAuthenticationFilter)在方法中successfulAuthentication.

因此,为了在情况 3 中进行其他重定向,您必须子类化SavedRequestAwareAuthenticationSuccessHandler并让它做你想做的事。


有时(取决于您的具体用例)启用useReferer的旗帜AbstractAuthenticationTargetUrlRequestHandler这是由SimpleUrlAuthenticationSuccessHandler(超类的SavedRequestAwareAuthenticationSuccessHandler).

<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Security登录成功后重定向到上一页 的相关文章

随机推荐