HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🍗
[New] 조규현팀
/
🐾
DailyLog
/
🔐
Provider는 Bean으로 등록 시 자동 등록 된다.
🔐

Provider는 Bean으로 등록 시 자동 등록 된다.

태그
SpringSecurity
날짜
May 26, 2022 08:04 AM
해결 상태
완료
Thrower
오늘자 강의중 두 번째 강의 수강 중 발생한 문제입니다.
 
Jwt 방식으로 필터를 구현해서 인증 처리를 구현하는 과정 중, 강의에서는 WebSecurityConfigure에 다음과 같은 설정들을 했습니다.
 
@Bean public JwtAuthenticationProvider jwtAuthenticationProvider(UserService userService, Jwt jwt) { return new JwtAuthenticationProvider(jwt, userService); } @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Autowired public void configureAuthentication(AuthenticationManagerBuilder builder, JwtAuthenticationProvider authenticationProvider) { builder.authenticationProvider(authenticationProvider); } public JwtAuthenticationFilter jwtAuthenticationFilter() { Jwt jwt = getApplicationContext().getBean(Jwt.class); return new JwtAuthenticationFilter(jwtConfigure.getHeader(), jwt); }
 
하지만 다음과 같은 순환 참조 오류가 발생했습니다.
notion image
하나하나 설정을 지워보면서 실행해보던 중 congifureAuthentication 설정 부분을 지워주니 정상 작동 했습니다.
@Autowired public void configureAuthentication(AuthenticationManagerBuilder builder, JwtAuthenticationProvider authenticationProvider) { builder.authenticationProvider(authenticationProvider); }
 
디버깅을 해보니 AuthenticationProvider를 구현한 JwtAuthenticationProvider를 Bean으로 등록 시 자동으로 AuthenticationManager에 함께 provider 등록이 되는 것을 알 수 있었습니다.
@Bean public JwtAuthenticationProvider jwtAuthenticationProvider(UserService userService, Jwt jwt) { return new JwtAuthenticationProvider(jwt, userService); }
notion image
 
그러므로 congifureAuthentication 설정 부분 코드는 삭제해도 무방했습니다.
강의에서는 SpringBoot 2.5.3버전을 사용했고, 제가 사용한 2.6.7버전에서는 기본적으로 순환 참조를 유발하는 코드 작성 시 애플리케이션 실행이 되지 않도록 되어있습니다.
 
순환 참조를 허용하고자 한다면 다음과 같은 옵션을 application.yaml에 설정해야하는 듯 합니다
spring: main: allow-circular-references: true
 
다만 제 생각에는 스프링에서 위험하다고 간주를 하여 막아놓은 순환 참조를 설정으로 강제로 허용을 한다는 것은 별로 좋지 않다고 생각이 들었습니다.