Use AuthenticationSuccessHandler implementation provided by Spring Security.
@Component
public class SimpleAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
@Override
public void onAuthenticationSuccess(HttpServletRequest arg0, HttpServletResponse arg1, Authentication authentication)
throws IOException, ServletException {
Collectionextends GrantedAuthority> authorities = authentication.getAuthorities();
authorities.forEach(authority -> {
if(authority.getAuthority().equals("ROLE_USER")) {
try {
redirectStrategy.sendRedirect(arg0, arg1, "/user");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if(authority.getAuthority().equals("ROLE_ADMIN")) {
try {
redirectStrategy.sendRedirect(arg0, arg1, "/admin");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
throw new IllegalStateException();
}
});
}
}
Ref -
Spring security login redirect