순환 참조 오류
The dependencies of some of the beans in the application context form a cycle:
2023-01-11 17:23:41.435 ERROR 10128 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| securityConfig (field private com.jig.security1.oauth.PrincipalOauth2UserService com.jig.security1.config.SecurityConfig.principalOauth2UserService)
↑ ↓
| principalOauth2UserService (field private org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder com.jig.security1.oauth.PrincipalOauth2UserService.bCryptPasswordEncoder)
└─────┘
Action:
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
[사유]
SecurityConfig에서 BCryptPasswordEncoder 빈 생성, PrincipalOauth2UserService를 참조함.
PrincipalOauth2UserService에서 BCryptPasswordEncoder 빈 @Autowired로 주입 받을 때 BCryptPasswordEncoder 가 생성된 SecurityConfig를 참조함.
결과적으로 SecurityConfig가 PrincipalOauth2UserService를 참조하고 PrincipalOauth2UserService에서도 SecurityConfig를 참조
[해결법 ]
BCryptPasswordEncoder 빈 생성을 다른 곳에서 하면 됨.
저는 BCryptPasswordEncoder 빈 생성을 목적으로 하는 config 클래스 추가로 만듬.
문제 코드
@Configuration
@EnableWebSecurity // 스프링 시큐리티 필터가 스프링 필터체인에 등록됩니다.
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) // securedEnabled = true -> @Secured 어노테이션 활성화 // prePostEnabled = true -> @PreAuthorize, @PostAuthorize 어노테이션 활성화
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public BCryptPasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
}
@Service
public class PrincipalOauth2UserService extends DefaultOAuth2UserService {
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
해결 코드
@Configuration
@EnableWebSecurity // 스프링 시큐리티 필터가 스프링 필터체인에 등록됩니다.
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) // securedEnabled = true -> @Secured 어노테이션 활성화 // prePostEnabled = true -> @PreAuthorize, @PostAuthorize 어노테이션 활성화
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/*
@Bean
public BCryptPasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}*/
}
@Configuration
public class PwdConfig {
@Bean
public BCryptPasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
}
'programming study > B-Spring Security' 카테고리의 다른 글
spring security와 OAuth2 (0) | 2023.01.14 |
---|---|
스프링 시큐리티 사용 이유 (0) | 2023.01.13 |
spring security config 설정할 때 WebSecurityConfigurerAdapter 가 Deprecated 된다. 해결법 (0) | 2023.01.09 |
Spring Security 로그인 동작 흐름 (0) | 2022.12.29 |
Spring Security 사용하기 (0) | 2022.12.27 |