If you want to control the navigation flow after successful authentication, you can do this by adding your own AuthenticationSuccessHandler.
Add the following attribute to your <form-login> element that refers to the customAuthenticationHandler bean,
<form-login login-page="/login.xhtml" authentication-success-handler-ref="customAuthenticationHandler"/> ... </http> <beans:bean id="customAuthenticationHandler" class="com.examples.CustomAuthenticationHandler" />
The CustomAuthenticationHandler class is as follows:
public class CustomAuthenticationHandler extends SimpleUrlAuthenticationSuccessHandler{ @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException { String userTargetUrl = "/welcome.xhtml"; String adminTargetUrl = "/admin/welcome.xhtml"; Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities()); if (roles.contains("ROLE_ADMIN")) { getRedirectStrategy().sendRedirect(request, response, adminTargetUrl); } else if(roles.contains("ROLE_USER")) { getRedirectStrategy().sendRedirect(request, response, userTargetUrl); } else { super.onAuthenticationSuccess(request, response, authentication); return; } } }
source share