Spring Security 3的AuthenticationSuccessHandler示例

时间:2011-09-19 11:45:48

标签: java spring

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

我想根据该用户的角色将用户重定向到不同的页面,我理解的是我必须实现相同的AuthenticationSuccessHandler,但是在这个方向上的一些示例会有所帮助。 / p>

提前致谢, 的Vivek

1 个答案:

答案 0 :(得分:23)

您可以这样做:

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"/>