j_spring_security_check后重定向

KVI*_*ISH 2 spring-security jsf-2

我能够通过以下方式获得Spring安全性:

<http access-denied-page="/start.jsf">
    <intercept-url pattern="/start.jsf" filters="none" />
    <intercept-url pattern="/web/**" access="ROLE_USER" />
    <form-login login-page="/start.jsf" default-target-url="/web/user/homepage.jsf"
            authentication-success-handler-ref="successHandler" always-use-default-target="true"
            authentication-failure-url="/index.jsf?state=failure"/>
    <logout logout-success-url="/index.jsf?state=logout" />
</http>

<beans:bean id="successHandler" class="com.myapp.security.MyAuthenticationSuccessHandler"/>
Run Code Online (Sandbox Code Playgroud)

我的问题是MyAuthenticationSuccessHandler类,在进行身份验证后,它只是一个空白页面.我可以使用context.redirect()重定向到默认主页,但有没有办法让它自动转到默认主页?我甚至将它列在spring xml中.

小智 6

这可能不起作用的原因之一是因为您的com.myapp.security.MyAuthenticationSuccessHandler没有扩展org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler或在#onAuthenticationSuccess内部重定向的任何其他身份验证处理程序.

您可以通过让您的服务扩展为您执行此操作而无需手动重定向,从而使其工作.例如...

@Service("authenticationSuccessHandler")
public class WebAuthenticationSuccessHandlerImpl extends SavedRequestAwareAuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //do your work here then call super so it redirects accordingly
        super.onAuthenticationSuccess(request, response, authentication);
    }
}
Run Code Online (Sandbox Code Playgroud)