Spring Security 3的AuthenticationSuccessHandler示例 [英] AuthenticationSuccessHandler example for Spring Security 3
本文介绍了Spring Security 3的AuthenticationSuccessHandler示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Spring Security 3的新手。我正在使用角色供用户登录。
I am a newbie to Spring Security 3 . I am using roles for users to login.
我想根据用户的角色将用户重定向到不同的页面,据我所知,我必须为此实现 AuthenticationSuccessHandler
,但在该方向上的一些示例会有所帮助。
I want to redirect a user to a different page based on the role of that user, I understand is that I would have to implement the AuthenticationSuccessHandler
for the same, but some examples in that direction would help.
提前致谢,
Vivek
Thanks in advance, Vivek
推荐答案
你可以这样做:
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 3的AuthenticationSuccessHandler示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文