본문 바로가기
트러블슈팅

[Spring Security] 사이클 에러 , The dependencies of some of the beans in the application context form a cycle: (스프링 시큐리티 에러)

by pyogowoon 2023. 1. 31.

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  securityConfig defined in file [C:\Users\dnflt\Downloads\***\***\out\production\classes\com\pyo\***\config\SecurityConfig.class]
↑     ↓
|  OAuth2DetailsService defined in file [C:\Users\dnflt\Downloads\***\***\out\production\classes\com\pyo\pyostagram\config\oauth\OAuth2DetailsService.class]
└─────┘


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.

 

 

해당 오류는 DI의 사이클 문제로

 어노테이션으로 bean 등록된 어노테이션 간의 참조순환 문제입니다.

 

저의 경우는

 

@RequiredArgsConstructor
@Service
public class OAuth2DetailsService extends DefaultOAuth2UserService {


    private final UserRepository userRepository;

    private final BCryptPasswordEncoder bCryptPasswordEncoder;
    
    
    
    										.
                                            .
                                            .
                                            .
                                            .
                                            .



@Override
    public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {

        OAuth2User oauth2User = super.loadUser(userRequest);

        Map<String, Object> userInfo = oauth2User.getAttributes();
        String name = (String) userInfo.get("name");
        String email = (String) userInfo.get("email");
        String username = "facebook_" + (String) userInfo.get("id");
        
        
        /*------------------------------문제발생----------------------------*/
        
        String password = bCryptPasswordEncoder.encode(UUID.randomUUID().toString());

 

소셜 로그인을 구현하는 도중 SecurityConfig 에 있는 

BCryptPassworEncoder를 가져오는 과정에서 오류가 발생하였는데

 그 이유는 

SecurityConfig의 cofigure 메소드에서 BCryptPassworEncoder를 참조하고

OAuth2DetailsService에서  BCryptPassworEncoder를 사용하기 위해

또다시 SecurityConfig를 참조하기 때문에 에러가 발생한 것입니다.

 

 

 

해결방법으론

application.yml

spring:
  application:
    name: //개인프로젝트//
  main:
    allow-circular-references: true

 

allow-circular-references 를 true로 설정하거나 

 

 

 

@RequiredArgsConstructor
@Service
public class OAuth2DetailsService extends DefaultOAuth2UserService {


    private final UserRepository userRepository;


    										.
                                            .
                                            .
                                            .
                                            .
                                            .



@Override
    public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {

        OAuth2User oauth2User = super.loadUser(userRequest);

        Map<String, Object> userInfo = oauth2User.getAttributes();
        String name = (String) userInfo.get("name");
        String email = (String) userInfo.get("email");
        String username = "facebook_" + (String) userInfo.get("id");
        
        
        /*------------------------------문제해결----------------------------*/
        
        String password = new BCryptPasswordEncoder().encode(UUID.randomUUID().toString());

 

BCrpytPasswordEncorder 의 의존성 주입을 없애고

따로

 new 객체로 생성하여 해결.

댓글