2011-09-19 75 views
8

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

我想根据用户的角色将用户重定向到不同的页面,据我所知,我将不得不实施AuthenticationSuccessHandler为相同的,但在这方面的一些例子会有所帮助。

由于提前, 维韦克

回答

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

ok了,在春天的安全性。 XML,我怎么能包括这个自定义处理程序(测试) – Vivek

+0

我已经相应地更新了我的答案。 – nfechner

+0

非常感谢您的帮助。它像一个魅力:) – Vivek