14.순환참조 문제

Alex·2024년 6월 22일
0

이슈 트래커

목록 보기
10/21
@Configuration
public class SpringSecurityConfig {

    @Autowired
    private  AuthenticationProvider authenticationProvider;


    @Bean
    SecurityFilterChain configure(HttpSecurity http) throws Exception {
        http.httpBasic(Customizer.withDefaults());

        http.authenticationProvider(authenticationProvider);

        http.authorizeHttpRequests(c->
                c.anyRequest().authenticated()
        );


        return http.build();
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}


@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        String username = authentication.getName();
        String password = authentication.getCredentials().toString();

        UserDetails u = userDetailsService.loadUserByUsername(username);

        if(passwordEncoder.matches(password, u.getPassword())) {

            return new UsernamePasswordAuthenticationToken(
                    username
                    , password
                    , u.getAuthorities());
        } else {
            throw new BadCredentialsException("계정 정보가 일치하지 않습니다."
            );
        }
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return authentication.equals(UsernamePasswordAuthenticationFilter.class);
    }
    
  

이 두개 클래스가 순환참조가 발생했다.
처음에는 생성자에 주입하는 방식이라서 필드로 주입하는 방식을 써도 여전히 순환참조가 일어났다.

순환참조는 a클래스가 b클래스를 참조하고, b클래스가 a 클래스를 참조할 때 발생한다.

이 문제는


@Configuration
public class SpringSecurityConfig {
    
    @Bean
    SecurityFilterChain configure(HttpSecurity http, AuthenticationProvider authenticationProvider) throws Exception {
        http.httpBasic(Customizer.withDefaults());

        http.authenticationProvider(authenticationProvider);

        http.authorizeHttpRequests(c->
                c.anyRequest().authenticated()
        );


        return http.build();
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

AuthenticationProvider를 매개변수로 넘겨주는 방법을 썼다. 스프링이 알아서 주입을 해주기 때문이다.


@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {


    private UserDetailsService userDetailsService;

    private PasswordEncoder passwordEncoder;

    public CustomAuthenticationProvider(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {
        this.userDetailsService = userDetailsService;
        this.passwordEncoder = passwordEncoder;
    }

이렇게만 해도 빈으로 등록이 되는데 config에 등록을 해야한다고 잘못 알고 있었다.

profile
답을 찾기 위해서 노력하는 사람

0개의 댓글