How to request authentication to all routes except welcome which has to be the login page in Spring Boot

288 Views Asked by At

I want to request authentication to all available routes except one "/welcome" which has to be the login page too!

I'm using Spring Boot Security and my SecurityFilterChain is coded like this:

@Configuration
public class AppConfig {

    @Bean
    SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeHttpRequests()
                .requestMatchers("/welcome").permitAll()
                .requestMatchers("/**").authenticated()
                .and().formLogin().loginPage("/welcome")
                .and().httpBasic();
        return http.build();
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

I want the user to be redirected to the welcome url/page when not authenticated. And when authenticated to be redirected to the root "/". However with this configuration the server keeps telling me ERR_TOO_MANY_REDIRECTS

Where am I doing wrong? How to allow public access only to the "/welcome" url and not the rest?

0

There are 0 best solutions below