小编典典

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

spring

我知道之前曾有人问过这个问题,但是我在这里面临一个特殊的问题。

我使用Spring Security 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错误。

编辑:

多亏了Ralph,我才找到了解决方案。这就是问题:我使用了物业

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

这里的解决方案非常明显:登录页面仅允许匿名用户使用。连接用户后,错误处理程序会将其重定向到主页。


阅读 532

收藏
2020-04-12

共1个答案

小编典典

登录后(将用户重定向到哪个url)发生什么情况,由处理AuthenticationSuccessHandler

该接口(实现它的具体类SavedRequestAwareAuthenticationSuccessHandler)由方法中的AbstractAuthenticationProcessingFilter或子类之一(UsernamePasswordAuthenticationFilter)调用successfulAuthentication。

因此,为了在情况3中进行其他重定向,你必须继承子类SavedRequestAwareAuthenticationSuccessHandler并使其执行你想要的操作。

有时(取决于你的确切用例)足以启用由(的的超类)调用其useReferer标志。AbstractAuthenticationTargetUrlRequestHandlerSimpleUrlAuthenticationSuccessHandlerSavedRequestAwareAuthenticationSuccessHandler

<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>
2020-04-12