티스토리 뷰
프로젝트 진행 중 순환 참조 오류가 발생했다.
에러 메세지를 읽어 보면 principalOAuth2Service 와 securityConfig 가 서로 순환참조를 하고 있기 때문에 실패했다고 한다.
내가 알기로 설정에서 이걸 그냥 무시할수도 있기는 한데 원인과 해결방법을 기록한다.
SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig {
private final PrincipalOAuth2UserService principalOAuth2UserService;
@Autowired
public SecurityConfig(PrincipalOAuth2UserService principalOAuth2UserService) {
this.principalOAuth2UserService = principalOAuth2UserService;
}
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
// ....
}
PrincipalOAuth2UserService.java
@Service
@Slf4j
public class PrincipalOAuth2UserService extends DefaultOAuth2UserService {
private final PasswordEncoder passwordEncoder;
private final MemberService memberService;
@Autowired
public PrincipalOAuth2UserService(PasswordEncoder passwordEncoder, MemberService memberService) {
this.passwordEncoder = passwordEncoder;
this.memberService = memberService;
}
// ...
}
원인
- 흐름을 생각해보면 빌드가 시작되고 스프링 컨테이너가 SecurityConfig 를 Bean 으로 등록하려고 할 것이다.
- 그런데 SecurityConfig 은 PrincipalOAuth2Service 에 의존하고 있기 때문에 PrincipalOAuth2Service 을 생성해야 한다.
- 자 그래서 PrincipalOAuth2Service 로 갔는데 PrincipalOAuth2Service 은 PasswordEncoder 빈이 필요하다.
- 그런데 PasswordEncoder 는 SecurityConfig 빈이 존재해야 생성할수 있다.
따라서 순환 참조가 발생하는 것이다.
해결
지금 문제는 PasswordEncoder 가 SecurityConfig 에서 @Bean 으로 설정하고 있기 때문에 발생하는 것이다.
따라서 AppConfig.java 같은 다른 클래스에서 PasswordEncoder 빈을 설정하면 된다.
애초에 이 문제가 발생한 이유는 Spring Security 설정 클래스인 SecurityConfig.java 에서 빈을 설정했기 때문이다.
그런데 스프링 시큐리티를 좀 사용해보니까 지금처럼 SecurityConfig 에는 @Bean 으로 새로운 빈을 설정하지 않고 AppConfig 같은 다른 곳에서 설정하고, 필요하면 주입 받는게 좋을것 같다.
'Web' 카테고리의 다른 글
thymeleaf) 객체의 리스트 클라이언트에 전달하기 (0) | 2024.02.08 |
---|---|
InstawebV2 완료, 변경점 (0) | 2023.12.28 |
OAuth2 로그인 시스템 구조 리팩토링 (0) | 2023.12.06 |
Spring WebClient (0) | 2023.11.13 |
git 프로젝트의 secret key 관리, heroku 배포 시 secret key 관리 (0) | 2023.11.13 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Kruskal
- binary search
- priority queue
- greedy
- floyd warshall
- CSS
- recursion
- Implementation
- db
- MVC
- Spring
- Dijkstra
- graph
- Stack
- dfs
- C++
- permutation
- 이분탐색
- C
- 조합
- back tracking
- 재귀
- Unity
- two pointer
- DP
- Brute Force
- 자료구조
- Python
- BFS
- Tree
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
글 보관함