Authentication, UserDetails

인증이 성공한 후에 , SecurityContextHolder가 담고 있는 SecurityContext 안의 Authentication 객체에는, 그 Authentication 객체의 Principal로서 '인증된 사용자의 모든 상세 정보가 담긴 UserDetails 객체'가 들어있는 것이다.

  1. 사용자가 아이디와 비밀번호를 입력해서 로그인 요청을 보내면, 스프링 시큐리티는 여기서 아이디와 비밀번호를 추출해서 '아직 인증되지 않은' Authentication 객체를 만들어. (이 Authentication 객체에는 사용자가 입력한 아이디/비밀번호가 Principal/Credentials에 임시로 담겨 있고 isAuthenticated()false)

  2. Authentication 객체가 AuthenticationManager를 거쳐 AuthenticationProvider에게 전달돼.

  3. AuthenticationProvider는 이 Authentication 객체에서 사용자 아이디를 꺼내서 UserDetailsService에게 "이 아이디를 가진 사용자의 상세 정보(UserDetails) 좀 로딩해줘!" 라고 요청해.

  4. UserDetailsService가 데이터베이스 등을 뒤져서 해당 사용자의 UserDetails 객체를 찾아서 반환해줘! (이 UserDetails 객체에는 데이터베이스에 저장된 '실제' 비밀번호 해시값, 권한 목록 등이 들어있지!)

  5. AuthenticationProvider는 사용자가 입력한 비밀번호와 UserDetailsService가 로드해온 UserDetails 객체의 비밀번호를 비교하는 등 실제 인증(신원 확인) 로직을 수행해.

  6. 만약 인증에 성공하면! AuthenticationProviderisAuthenticated()true로 설정된 '새로운' Authentication 객체를 만들어서 반환해! 그리고 이 새로운 Authentication 객체의 Principal 필드에 방금 UserDetailsService로부터 로드해온 그 'UserDetails 객체'를 담아줘! 🎉 또한 UserDetails 객체의 getAuthorities() 메소드를 호출해서 권한 목록을 가져와 이 Authentication 객체의 Authorities 필드에 채워 넣지!

Last updated