UserDetailsService

황상익·2024년 9월 19일

security

목록 보기
9/16

사용자와 관련된 상세 데이터를 로드하는 것, 사용자의 신원, 권한, 자격증명 → 정보 포함
인터페이를 사용하는 클래스는 주로 AuthenticationProvider를 사용, 사용자가 시스탬에 존재하는지 여부와 사용자 데이터를 검색하고 인증 과정을 수행

user가 없다면 UserNotFoundException 인증에 실패하도록 설계 되어 있음

UserDetailsService 사용 방법

@Bean //일반 객체 사용 
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
AuthenticationManagerBuilder managerBuilder = http.getSharedObject(AuthenticationManagerBuilder.class);
managerBuilder.userDetailsService(customUserDetailsService()); http.userDetailsService(customUserDetailsService());
//모두 동일한 처리를 한다
http.authorizeHttpRequests(auth -> auth.anyRequest().authenticated()); http.formLogin(Customizer.withDefaults());
return http.build(); }

@Bean //Bean으로 사용 
public UserDetailsService customUserDetailsService(){ return new CustomUserDetailsService();
}

→ UserDetailsService만 커스트마이징 할 경우 이와 같이 적용

→ AuthenticationProvider와 함께 커스트마이징, AuthenticationProvider에 직접 주입해서 사용

profile
개발자를 향해 가는 중입니다~! 항상 겸손

0개의 댓글