• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

CustomAuthenticationProvider 질문

23.08.25 21:01 작성 조회수 640

0

CustomAuthenticationProvider를 작성한 이후, SecurityConfig를 등록할 때 Adapter를 쓰지 않았을 경우 어떻게 등록하는지 알고 싶습니다.

Adapter를 찾아보니 deprecated되어서 사용하지 않고 filterChain을 사용했을 때 어떻게 등록해야한다 이런 정보가 없어서요.

 

답변 2

·

답변을 작성해보세요.

1

방법은 몇 가지가 있는데 기억할 점은 방법에 따라 결과가 약간 다르게 나옵니다

첫번째로 httpsecurity.authenticationProvider(new CustomAuthenticationProvider()) 로 설정할 수 있습니다. 이렇게 할 때는 CustomAuthenticationProvider 를 빈이 아닌 일반객체로 생성하는 식으로 합니다.

두번째는 빈으로 설정하는 겁니다.

@Bean
    public CustomAuthenticationProvider customAuthenticationProvider1(){
        return new CustomAuthenticationProvider();
    } 

빈으로 설정하게 되면 시큐리티가 자동적으로 AuthenticationProvider 를 추가 해 줍니다.

세번째는 AuthenticationManagerBuilder 를 사용해서 등록하는 건데요

AuthenticationManagerBuilder authenticationManagerBuilder = http.getSharedObject(AuthenticationManagerBuilder.class);
authenticationManagerBuilder.authenticationProvider(new CustomAuthenticationProvider())

위 세가지 방식에서 한 가지를 택하면 되는데 세 가지 모두 시큐리티에서 작동하는 방식이 조금씩 다릅니다.

그래서 간혹 어떤 경우에는 추가한 AuthenticationProvider 가 우선적으로 실행되지 않는 현상이 발생하기도 합니다.

일단 위의 방식을 참조하셔서 진행해 보시기 바랍니다.

참고적으로 AuthenticationManager 객체를 구하기 위해서는 다음과 같이 할 수 있습니다.

AuthenticationManagerBuilder authenticationManagerBuilder = http.getSharedObject(AuthenticationManagerBuilder.class);
AuthenticationManager authenticatonManager = authenticationManagerBuilder.build();

혹은

@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {

    ProviderManager authenticationManager = (ProviderManager)authenticationConfiguration.getAuthenticationManager();
    return authenticationManager;

}

인데 위 두가지도 동일한 객체를 반환 하지는 않습니다.

그래서 두 개 중에 하나를 선택해서 실행해 보면서 정상적으로 결과가 나오는지 봐야 할 경우도 있습니다.

-1

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2023.08.27

안녕하세요, 인프런 AI 인턴이에요.

CustomAuthenticationProvider를 작성한 후, SecurityConfig에 Adapter를 사용하지 않고 filterChain을 이용해 등록하는 방법에 대해 알고 싶으시군요.

CustomAuthenticationProvider를 사용하는 경우, 보통은 CustomWebSecurityConfigurerAdapter를 상속받아서 SecurityConfig를 작성합니다. 이때 filterChain을 사용하려면, 설정 클래스에서 HttpSecurity를 이용하여 필터를 등록하면 됩니다.

예를 들어, 다음과 같이 SecurityConfig를 작성할 수 있습니다:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomAuthenticationProvider customAuthenticationProvider;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authenticationProvider(customAuthenticationProvider)
            .authorizeRequests()
                .antMatchers("/public/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/home")
                .permitAll()
                .and()
            .logout()
                .logoutUrl("/logout")
                .permitAll();
    }
}

위 예시 코드에서 customAuthenticationProviderCustomAuthenticationProvider를 구현한 객체입니다. http.authenticationProvider() 메서드를 사용하여 해당 프로바이더를 등록할 수 있습니다.

그리고 http.authorizeRequests() 메서드를 이용하여 URL 접근 권한을 설정할 수 있습니다. 필요한 경로나 패턴에 대해 permitAll() 또는 authenticated() 메서드를 이용하여 접근 제한을 설정할 수 있습니다.

마지막으로 http.formLogin() 메서드를 이용하여 로그인 페이지와 로그인 성공 후 이동할 페이지를 설정할 수 있습니다. .permitAll()을 이용하여 로그인 페이지 접근 권한을 설정합니다.

위의 예시는 기본적인 설정이므로, 필요에 맞게 수정하여 사용하실 수 있습니다.

더 자세한 내용은 Spring Security 공식 문서나 관련 강의를 참고하시면 도움이 될 것입니다. 감사합니다!