2016-04-29 7 views
0

タイル設定で定義されたページにリダイレクトする方法を習っています。春の注釈付きセキュリティ4とタイル3スプリング4セキュリティタイル3カスタムサクセスハンドラ

作品以下CustomSuccessHandlerが、それは、タイルの設定で定義されたページへtargetUrlを解決しませんを使用して

@Component 
public class CustomSuccessHandler extends SimpleUrlAuthenticationSuccessHandler{ 

private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 


@Override 
protected void handle(HttpServletRequest request, 
    HttpServletResponse response, Authentication authentication) throws IOException { 
    String targetUrl = determineTargetUrl(authentication); 

    if (response.isCommitted()) { 
     System.out.println("Can't redirect"); 
     return; 
    } 
    test(); 
    redirectStrategy.sendRedirect(request, response, targetUrl); 
} 
static void test() { 

} 

protected String determineTargetUrl(Authentication authentication) { 
    String url=""; 

    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); 

    List<String> roles = new ArrayList<String>(); 

    for (GrantedAuthority a : authorities) { 
     roles.add(a.getAuthority()); 
    } 

    if (isAdmin(roles)) { 
     url = "/admin"; 
    } else if (isUser(roles)) { 
     url = "/user"; 
    } else { 
     url="accessDenied"; 
    } 

    return url; 
} 

答えて

0

私は自分の問題はいつものように自力であったと考えました。私は、上記の "admin"または "user"をviews.xml(タイル設定)ファイルで定義することを怠っていました。 views.xmlでページを構成すると、期待どおりに動作し始めました。ありがとう!