【发布时间】:2011-11-20 04:31:13
【问题描述】:
我是 Spring Security 3 的新手。我正在使用角色让用户登录。
我想根据该用户的角色将用户重定向到不同的页面,我知道我必须为此实现 AuthenticationSuccessHandler,但该方向的一些示例会有所帮助。
提前致谢, 维韦克
【问题讨论】:
我是 Spring Security 3 的新手。我正在使用角色让用户登录。
我想根据该用户的角色将用户重定向到不同的页面,我知道我必须为此实现 AuthenticationSuccessHandler,但该方向的一些示例会有所帮助。
提前致谢, 维韦克
【问题讨论】:
你可以这样做:
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"/>
【讨论】: