Spring Security 3的AuthenticationSuccessHandler示例

Viv*_*vek 8 java spring

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

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

提前谢谢,Vivek

nfe*_*ner 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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在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"/>
Run Code Online (Sandbox Code Playgroud)