【问题标题】:AuthenticationSuccessHandler example for Spring Security 3Spring Security 3 的 AuthenticationSuccessHandler 示例
【发布时间】:2011-11-20 04:31:13
【问题描述】:

我是 Spring Security 3 的新手。我正在使用角色让用户登录。

我想根据该用户的角色将用户重定向到不同的页面,我知道我必须为此实现 AuthenticationSuccessHandler,但该方向的一些示例会有所帮助。

提前致谢, 维韦克

【问题讨论】:

    标签: java spring


    【解决方案1】:

    你可以这样做:

    public class Test implements AuthenticationSuccessHandler {
        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
            Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
            if (roles.contains("ROLE_USER") {
                response.sendRedirect("/userpage");
            }
        }
    }
    

    在 XML 配置中添加:

    <bean id="authenticationFilter" class="YOUR_AUTH_FILTER_HERE">
        <!-- There might be more properties here, depending on your auth filter!! -->
        <property name="authenticationSuccessHandler" ref="successHandler" />
    </bean> 
    
    <bean id="successHandler" class="Test"/>
    

    【讨论】:

    • 好的,在 spring-security.xml 中,我怎样才能包含这个自定义处理程序(测试)
    • 解决问题的最酷方法。非常感谢。我找了一整天,终于得到了这个帖子。非常感谢,你也解决了我的问题!!!
    猜你喜欢
    • 2011-09-19
    • 2016-07-17
    • 2014-01-04
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 2019-06-24
    • 2014-08-06
    相关资源
    最近更新 更多