Spring Boot 프로젝트에서 Spring Security를 설정하다 보면 다음과 같은 에러를 만날 수 있다.

An AuthenticationManager is required. Consider defining a bean of type AuthenticationManager. 
... needs to have a non-void return type!

 

 

원인

1. SecurityFilterChain 설정 문제

  • @Bean으로 등록하는 SecurityFilterChain 메서드는 반드시 SecurityFilterChain 타입을 반환해야 한다.
  • 반환 타입이 void 또는 잘못된 타입인 경우 에러가 발생한다.

2. AuthenticationManager 설정 문제

  • AuthenticationManager를 빈으로 등록할 때도 정확한 반환 타입을 지정해야 한다.

 

해결 방법

1. SecurityFilterChain 반환 타입 확인

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http
        .authorizeHttpRequests(auth -> auth
            .requestMatchers("/public/**").permitAll()
            .anyRequest().authenticated()
        )
        .formLogin()
        .and()
        .cors(withDefaults())
        .httpBasic();
    return http.build();  // 이 부분을 반환하는 지 확인해야 함
}

 

2. Spring Boot 버전 확인

  • 최신 버전의 Spring Boot에서는 Security 설정 방식이 변경되었을 수 있으므로 공식문서 확인하여 변경한다. 

+ Recent posts