@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 = targetUrl(authentication);
if (response.isCommitted()) {
System.out.println("Can't redirect");
return;
}
redirectStrategy.sendRedirect(request, response,
targetUrl);
}
protected String targetUrl(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 (isUserRole(roles)) {
url = "/deptform.html";
} else if (isAdminRole(roles)){
url = "/deptform.html";
} else if (isHrAdminRole(roles)){
url = "/deptform.html";
} else{
url = "/deptform.html";
}
return url;
}
}