2016-04-29 118 views
0

我有一個粗略的時間搞清楚如何重定向到瓷磚配置中定義的頁面。春季4安全瓷磚3定製成功處理程序

以Spring Security 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

我像往常一樣發現自己的問題是自己造成的。我忽略了在我的views.xml(tiles配置)文件中定義上面的「admin」或「user」。一旦我在views.xml中配置頁面,它就按預期開始工作。謝謝!