简体   繁体   中英

Spring 4 Security Tiles 3 Custom Success Handler

I'm having a rough time figuring out how to redirect to a page defined in tiles configuration.

Using Spring Security 4 with annotations and Tiles 3.

The CustomSuccessHandler below works but it doesn't resolve the targetUrl to the page defined in tiles configuration.

@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;
}

I figured out that my problem was self-inflicted, as usual. I had neglected to define "admin"or "user" above, in my views.xml (tiles configuration) file. Once I configured the pages in views.xml, it started working as expected. Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM