j_spring_security_check后重定向

时间:2012-04-03 03:43:19

标签: jsf-2 spring-security

我能够通过以下方式获得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"/>

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

2 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:3)

尝试删除default-target-url属性并添加以下内容:

<beans:bean id="successHandler" class="com.myapp.security.MyAuthenticationSuccessHandler">
    <beans:property name="defaultTargetUrl" value="/web/user/homepage.jsf"/>
</beans:bean>

Spring安全文档说,当使用自定义身份验证成功处理程序时,您必须删除该属性并在处理程序中设置目标。

相关问题