Spring Boot Spring Security Change dst after login

Success Handler

We can change dst after login using Spring Security.
In this case, we need to extend SavedRequestAwareAuthenticationSuccessHandler.
We can get role and decide which role where goes


public class AuthSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response) {
       Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String role = auth.getAuthorities().toString();

        String targetUrl = "/";
        if (role != null) {
            if (role.contains("ADMIN")) {
                targetUrl = "/admin";
            else if (role.contains("USER")) {
                targetUrl = "/user/index";
            else {
                // Others
                targetUrl = "/";
        return targetUrl;

Point : Get role from context and check Role and handle destination.
ADMIN goes to /admin, USER goes to /user/index.

How to set role? This is next step. We can set role from configuration.

Set and Test

public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private AuthSuccessHandler authSuccessHandler;

    protected void configure(HttpSecurity http) throws Exception {
                .antMatchers("/css/**", "/index").permitAll()
                .antMatchers("/user/**").hasAnyRole(new String[]{"USER", "ADMIN"})

    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
                    .withUser("admin").password("admin").authorities(new String[]{"ADMIN"})

Disable csrf for Test.
/admin, Only users who have ADMIN authority can access.
/user, Only users who are USER or ADMIN role can access.
Those users authority are required to login.

Auth configure part, I prepared 3 users, USER role “user”, ADMIN auth “admin”, and general user “buruburu”


Access login localhost:8080/login, this is default login page.
If you can sign in as user or admin, you can access under /user, /admin.
For details

User Access point
user /api, /user
admin /api, /user, /admin
buruburu /api