2011-09-19 72 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

相關問題