注销后Spring Security 3.1重定向

Mat*_*Mat 4 redirect spring spring-security

我正在阅读很多教程,但没有一个能为我工作......我使用Spring 3.1.x和Spring Security.我有一堆安全网址,许多没有安全保护.现在,当用户登录并尝试注销时,我希望他保持与以前相同的页面,所以我使用它:

<beans:bean id="logoutSuccessHandler" class="org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler">
    <beans:property name="useReferer" value="true"/>
</beans:bean>
Run Code Online (Sandbox Code Playgroud)

这很好,但是当用户从安全页面注销时,它会将其重定向到登录页面,我想重定向到主页..我怎样才能实现这一点?提前致谢!

Nic*_*lbu 7

由于您具有重定向的自定义逻辑,因此您需要自定义LogoutSuccessHandler.在此处理程序中,您需要添加以下逻辑:

String refererUrl = request.getHeader("Referer");
String normalizedRefererUrl = ...; // "Normalize" (if needed) the URL so it is in the form that you need.

if (requiresAuthentication(normalizedRefererUrl, authentication)) {
    response.sendRedirect(request.getContextPath()); // home page
} else {
    response.sendRedirect(refererUrl); // or normalizedUrl
}
Run Code Online (Sandbox Code Playgroud)

在您的requiresAuthentication()方法中,您需要使用Spring Security的某些部分来确定URL是否需要身份验证.
你可以在WebInvocationPrivilegeEvaluator那里使用参考.你可以通过Spring通过类自动装配来获得它(因为将有一个bean实现WebInvocationPrivilegeEvaluator).
评估者有一个你可以使用的方法isAllowed(uri, authentication).


Nim*_*sky 5

<security:http auto-config="true">
    <security:form-login login-page="/spring/login" 
                         login-processing-url="/spring/loginProcess"
                         default-target-url="/spring/main" 
                         authentication-failure-url="/spring/login?login_error=1" />  
    <security:logout logout-url="/spring/logout" logout-success-url="/spring/logout-success" />
</security:http>
Run Code Online (Sandbox Code Playgroud)

来自文档自定义成功处理程序的logout-success-url