Spring Security Redirect Logged Users From Login/Register Page to Home

Viewed 15

I'm building a spring boot application with spring security in which I want to prevent logged-in users from viewing some specific pages and instead redirect them to another.

For example, if a logged-in user goes to /register or /login, he should be redirected to /home.

What is the best way to achieve this?

My config class looks like this:

@Configuration
@EnableWebSecurity
public class AppSecurityConfig
{
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .formLogin()
                    .loginPage("/login")
                    .loginProcessingUrl("/login-action")
                    .defaultSuccessUrl("/login-success")
                    .failureUrl("/login?error");

        return httpSecurity.build();
    }
}
0 Answers
Related