DEV Community

mauroreinehr
mauroreinehr

Posted on

Problems Starting Spring Boot Application

I'm trying to configure Token authentication in Spring Boot 2.7.1 with the following setting:

@Configuration
@RequiredArgsConstructor
@EnableWebSecurity
public class SecurityConfig {
    @Autowired
    private UserService userService;

    @Autowired
    private AuthEntryPointJwt unauthorizedHandler;

    @Bean
    AuthTokenFilter authenticationJwtTokenFilter() {
        return new AuthTokenFilter();
    }

    @Bean
    UserDetailsService userDetailsService() {
        return userService;
    }

    @Bean
    AuthenticationManager authenticationManager(AuthenticationManagerBuilder builder, PasswordEncoder encoder)
            throws Exception {
        return builder.userDetailsService(userService).passwordEncoder(encoder).and().build();
    }

    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable().exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests()
                .antMatchers("/api/v1/auth/**").permitAll().antMatchers("/api/v1/test/**").permitAll().anyRequest()
                .authenticated();

        http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);

        return http.build();
    }

}
Enter fullscreen mode Exit fullscreen mode

But when I start the application, you are getting the following error:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Unsatisfied dependency expressed through method 'setFilterChains' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'defaultSecurityFilterChain' defined in class path resource [org/springframework/boot/autoconfigure/security/servlet/SpringBootWebSecurityConfiguration$SecurityFilterChainConfiguration.class]: Unsatisfied dependency expressed through method 'defaultSecurityFilterChain' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.HttpSecurityConfiguration.httpSecurity' defined in class path resource [org/springframework/security/config/annotation/web/configuration/HttpSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.config.annotation.web.builders.HttpSecurity]: Factory method 'httpSecurity' threw exception; nested exception is java.lang.IllegalStateException: Cannot apply org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration$EnableGlobalAuthenticationAutowiredConfigurer@194e1965 to already built object

Top comments (1)

Collapse
 
rc242_ profile image
RC

You may be rebuilding the AuthenticationManager twice. Try changing

@Bean
AuthenticationManager authenticationManager(AuthenticationManagerBuilder builder, PasswordEncoder encoder) throws Exception {
    return builder.userDetailsService(userService).passwordEncoder(encoder).and().build();
}
Enter fullscreen mode Exit fullscreen mode

to

@Autowired
public void authenticationManager(AuthenticationManagerBuilder builder, PasswordEncoder encoder) {
    builder.userDetailsService(userService).passwordEncoder(encoder);
}
Enter fullscreen mode Exit fullscreen mode