2011-09-19 10 views
8

私はSpring Security 3の初心者です。私はユーザーがログインするための役割を使用しています。Spring Security 3のAuthenticationSuccessHandlerの例

私はそのユーザーの役割に基づいて別のページにリダイレクトしたいと思っています。私はAuthenticationSuccessHandlerを実装しなければならないことを理解していますが、その方向の例が役立ちます。事前に

おかげで、 のVivek

答えて

23

あなたはこのような何か行うことができます:XMLの設定で

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"); 
     } 
    } 
} 

この追加:OK、と春・セキュリティに

<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

を。 xml、どのように私はこのカスタムハンドラ(テスト)を含めることができます – Vivek

+0

私はそれに応じて私の答えを更新しました。 – nfechner

+0

ありがとうございました。それは魅力のように働いた:) – Vivek