• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

CustomAuthenticationProvider는 왜 Autowired를 사용할 수 없나요?

21.01.13 18:56 작성 조회수 639

0

provider를 Service로 등록하고 Autowired로 주입 받았을때 PasswordEncoder에서 문제가 생기는 것 같습니다 그런데 왜그런지 이해가 잘 가지 않습니다,

답변 4

·

답변을 작성해보세요.

0

JJ NAM님의 프로필

JJ NAM

질문자

2021.01.15

Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'passwordEncoder';

nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'customAuthenticationProvider';

nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customAuthenticationProvider': Unsatisfied dependency expressed through field 'passwordEncoder';

nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'passwordEncoder': Requested bean is currently in creation: Is there an unresolvable circular reference?

이런 에러가 뜹니다

0

지금 SecurityConfig 와 CustomAuthenticationProvider 를 보시면 CustomAuthenticationProvider 를 빈으로 생성하는 구문이 중복되어 있습니다.

@Configuration

@EnableWebSecurity

public class SecurityConfig extends WebSecurityConfigurerAdapter {

...

@Autowired

CustomAuthenticationProvider customAuthenticationProvider;

@Bean

    public AuthenticationProvider authenticationProvider() {

        return new CustomAuthenticationProvider();

    }

....

@Override

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.authenticationProvider(authenticationProvider());

    }

...

}

authenticationProvider() 를 통해 빈을 configure 에 설정하고 있습니다.

근데 

@Autowired

CustomAuthenticationProvider customAuthenticationProvider;

으로 DI 하고 있는데 이 구문은 필요없고 사용하지도 않습니다.

이미  authenticationProvider() 로 빈을 호출 하고 있기 때문입니다.

또한 CustomAuthenticationProvider  을 보시면

@Component

public class CustomAuthenticationProvider implements AuthenticationProvider {....}

CustomAuthenticationProvider  에서도 @Component 으로 빈으로 생성하고 있습니다.

즉 이것은 스프링 시큐리티의 문제가 아니라 스프링 빈의 lifecycle 과 관련된 문제이니 관련된 내용을 좀 더 보시기 바랍니다.

일단  SecurityConfig 의

@Bean

    public AuthenticationProvider authenticationProvider() {

        return new CustomAuthenticationProvider();

    }

을 삭제하시고 

@Override

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.authenticationProvider(customAuthenticationProvider);

    }

이렇게 변경해서 실행해 보시기 바랍니다.

 

0

JJ NAM님의 프로필

JJ NAM

질문자

2021.01.14

https://github.com/JeongJin984/SpringSecurity/blob/master/SpringSecurity2/src/main/java/com/example/SpringSecurity2/Security/SecurityConfig.java 여기에서 Provider @Autowired 부분에서 passwordEncoder가 오류가 납니다 

0

혹시 소스 좀 공유 부탁드립니다