【问题标题】:redirect after j_spring_security_check在 j_spring_security_check 之后重定向
【发布时间】:2012-04-03 03:43:18
【问题描述】:

我能够让 spring security 一切都与以下内容一起工作:

<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 中列出了它。

【问题讨论】:

    标签: jsf-2 spring-security


    【解决方案1】:

    这可能不起作用的原因之一是因为您的 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);
        }
    }
    

    【讨论】:

    • 先生,您救了我的命,谢谢。我认为这应该被标记为正确答案。
    【解决方案2】:

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

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

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

    【讨论】:

    • 那行不通。但我只能通过重定向让它工作。我基本上是在传递我想从 bean 中使用的 url。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    相关资源
    最近更新 更多