AuthenticationProvider

황상익·2024년 9월 19일

security

목록 보기
8/16

사용자의 자격 증명을 확인하고 인증 과정을 관리하는 클래스, 사용자가 시스탬에 엑세스 하기 위해 제공한 정보가 유효한지 검증하는 과정 포함
다양한 유형의 메커니즘을 지원, 사용자 이름과 비밀번호를 기반으로 한 인증, 토큰 기반 인증, 지문인식 등 처리
성공한 후 Authentication 객체를 반환, 객체에는 사용자의 신원 정보와 인증된 자격 증명(권한 정보)을 포함
인증 과정 중 문제 발생한 경우, AuthenticaitonException과 같은 예외를 발생, 문제를 알리는 역할

  • Authenticaiton으로 부터 Authentication 객체를 전달받아 인증을 수행
  • supports → 인증을 수행할 수 있는 조건인지 확인

AuthenticationProvider 사용방법
(일반 객체로 사용)

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
AuthenticationManagerBuilder managerBuilder = http.getSharedObject(AuthenticationManagerBuilder.class); managerBuilder.authenticationProvider(new CustomAuthenticationProvider()); http.authenticationProvider(new CustomAuthenticationProvider2());
http.authorizeHttpRequests(auth -> auth.anyRequest().authenticated()); http.formLogin(Customizer.withDefaults());
return http.build(); }

1) Bean으로 생성
→ 한개만 정의 한 경우 (AuthenticaitonProvider를 빈으로 정의하면 DaoAuthenticationProvder를 자동으로 대체)

@Bean

public AuthenticationProvider customAuthenticationProvider(){
return new CustomAuthenticationProvider();

}

@Bean

public SecurityFilterChain securityFilterChain(HttpSecurity http, AuthenticationManagerBuilder builder, AuthenticationConfiguration configuration)
throws Exception {

 (1) AuthenticationManagerBuilder managerBuilder = http.getSharedObject(AuthenticationManagerBuilder.class);
managerBuilder.authenticationProvider(customAuthenticationProvider()); //Bean 객체가 들어온다. 

 (2) ProviderManager providerManager = (ProviderManager)configuration.getAuthenticationManager();
providerManager.getProviders().remove(0); 
// providerManager → parent (DaoAuthenticaiton)을 넣기 위해서 삭제
builder.authenticationProvider(new DaoAuthenticationProvider());

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

return http.build();
}

→ 두개 이상으로 정의 (일반 객체를 생성하는 것과 동일)

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
AuthenticationManagerBuilder managerBuilder = http.getSharedObject(AuthenticationManagerBuilder.class); managerBuilder.authenticationProvider(customAuthenticationProvider()); managerBuilder.authenticationProvider(customAuthenticationProvider2());
http.authorizeHttpRequests(auth -> auth.anyRequest().authenticated()); http.formLogin(Customizer.withDefaults());
return http.build(); }

//ManagerBuilder에 넣으면 됨 
@Bean
public AuthenticationProvider customAuthenticationProvider(){ return new CustomAuthenticationProvider();
}
@Bean
public AuthenticationProvider customAuthenticationProvider2(){ return new CustomAuthentication
profile
개발자를 향해 가는 중입니다~! 항상 겸손

0개의 댓글