• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

CustomAuthenticationProvider 질문드립니다!

21.01.09 18:29 작성 조회수 422

0

안녕하세요 선생님

우선 좋은 강의 감사드립니다!

spring context에서 빈 생성 방법에 대해 질문드립니다

이전에 CustomAuthenticaionService는 @Service 어노테이션으로 빈으로 등록하고

SecurityConfig에서 @Autowird를 통해 주입 받은 빈을 

auth.userDetailsService에 전달해주셨습니다.

반면, CustomAuthenticationProvider는 

바로 밑에 빈 생성 매서드를 이용해 주입하셨고,

@Bean

CustomAuthenticationProvider customAuthenticationProvider(){

    return ...

}

저는 아무 생각 없이 CustomAuthenticationService처럼 @Service 어노테이션을 붙인 뒤

@Autowired를 이용해 Prover를 주입했습니다.

이때 무수히 많은 에러로그를 발생시키면서 프로그램이 종료되는데 혹시 원인을 알 수 있을까요??

감사합니다!

답변 2

·

답변을 작성해보세요.

0

cjswoudddn님의 프로필

cjswoudddn

질문자

2021.01.11

우선 답변 감사드립니다!

선생님이 적어주신 내용으로 문제가 해결되었습니다.

다만, 저는 CustomAuthenticaionProvider에 의존성주입을 생성자를 통해 해주었습니다.

그리고 @Service로 빈등록을 마친 후 다시 생성자를 통해 CustomAuthenticationProvider를 주입해주었습니다.

의존성 주입에서 문제가 생긴듯 합니다 ㅠㅠ 감사드립니다!

0

말씀하신 대로 해도 상관없습니다.

몇가지만 수정하시면 됩니다.

FormAuthenticationProvider 에 보시면 PasswordEncoder 에 @Autowired 를 설정해 주시고

@Service
public class FormAuthenticationProvider implements AuthenticationProvider {

@Autowired
private UserDetailsService userDetailsService;

@Autowired
private PasswordEncoder passwordEncoder;

SecurityConfig 에서

@Configuration
@EnableWebSecurity
@Slf4j
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private FormWebAuthenticationDetailsSource formWebAuthenticationDetailsSource;
@Autowired
private AuthenticationSuccessHandler formAuthenticationSuccessHandler;
@Autowired
private AuthenticationFailureHandler formAuthenticationFailureHandler;
@Autowired
private SecurityResourceService securityResourceService;
@Autowired
private AuthenticationProvider formAuthenticationProvider;

@Autowired
private AuthenticationProvider formAuthenticationProvider;

와 같이 추가해 주시면 됩니다.

그리고

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(formAuthenticationProvider);
}

와 같이 설정해 주시면 정상적으로 동작하고 있습니다.

혹시 잘 안되시면 github 에 소스를 공유해 주시면 제가 확인해 보겠습니다.