Spring 4安全性瓷砖3自定义成功处理程序

时间:2016-04-29 02:03:56

标签: java spring spring-mvc spring-security tiles-3

我很难搞清楚如何重定向到瓷砖配置中定义的页面。

将Spring Security 4与注释和Tile一起使用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;
}

1 个答案:

答案 0 :(得分:0)

我发现我的问题像往常一样是自我造成的。我忽略了定义&#34; admin&#34;或&#34; user&#34;上面,在我的views.xml(tiles配置)文件中。在views.xml中配置页面后,它开始按预期工作。谢谢!