Spring Security 이란?
Spring Security는 일반적인 공격에 대한 인증 , 권한 부여 및 보호를 제공하는 프레임워크입니다 . 명령형 애플리케이션 과 반응형 애플리케이션 모두를 보호하기 위한 일급 지원을 통해 Spring 기반 애플리케이션을 보호하기 위한 사실상의 표준입니다.
기본 용어
인증 (Authentication)
- 보호된 리소스(데이터 및 매서드)에 접근한 Request(요구자)가 이 사람이 회원인지, 회원이라면 어떤 정보(이름, 이메일) 가진 Request(요구자)인지를 확인하는 과정
인가 (Authorization)
- 인증 이후 해당 리소스에 대해 접근이 가능한 권한을 가졌는지를 확인하는 과정
동작 원리
1. Http Request 수신
2. 로그인 요청시 AuthenticationFilter가 HttpServletRequest의 ID, PassWord 인터셉트 후
UsernamePasswordAuthenticationToken을 만든다.
UsernamePasswordAuthenticationToken authenticationToken =
new UsernamePasswordAuthenticationToken(
loginRequestDto.getUsername(),
loginRequestDto.getPassword());
3. 이 토큰이 유효한지 AuthenticationManager에 위임한다.
AuthenticationManager는 Interface 이며, 실제 구현은 ProviderManager에 되어 있습니다.
ProviderManager.authenticate() 실행시, AuthenticationProvider를 순회하여 처리할 곳을 찾은 후 처리하게 됩니다.
※ 인증 프로바이더의 Default : UserDetailsService
Authentication authentication =
authenticationManager.authenticate(authenticationToken);
4. UserDetailsService를 implements 한 구현체에 loadUserByUsername() 오버라이드 된 메서드 실행
이곳에서 DB에 접근하여 유저의 정보를 갖고오게 됩니다.
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
return new PrincipalDetails(user);
}
5. 이후 인증객체 UserDetails를 반환 후, AuthenticationManager는 완벽한 인증개체를 관련
인증 필터(AuthenticationFilter)로 다시 반환한다
그리고 이 인증 정보를 SecurityContext에 저장하고 향후에는 인증 로직이 수행되지 않는다
SecurityContextHolder.getContext().setAuthentication(authentication);
대략적인 Spring Security의 인증 구조는 위와 같습니다.
이 포스팅은 간단한 개념 설명 및 동작 원리를 설명하는 포스팅이므로 세세한 모든 필터 종류 및 동작 순서는
차후에 다시 작성하도록 하겠습니다
또한, 잘못된 내용이 있으면 댓글 주시면 바로 수정하겠습니다. 감사합니다
해당 내용을 미리 공부하시고 싶으시면 Spring Security 공식문서 에서 가능합니다.
Architecture :: Spring Security
Spring Security’s Servlet support is based on Servlet Filters, so it is helpful to look at the role of Filters generally first. The picture below shows the typical layering of the handlers for a single HTTP request. The client sends a request to the appl
docs.spring.io
참고 문서
https://k3068.tistory.com/88#recentEntries
Spring security 동작 원리 (인증,인가)
인증(Authentication) 인증이란 식별 가능한 정보 (이름 , 이메일)를 이용하여 서비스에 등록 유저의 신원 입증하는 과정이다. 즉 나의 서비스에 등록된 사용자에게만 서비스를 제공한다는 뜻으로 간
k3068.tistory.com
'language > Spring' 카테고리의 다른 글
Spring Batch && Scheduler (0) | 2022.08.10 |
---|---|
[Spring] @Transactional 전파와 격리 (0) | 2022.07.05 |
[JPA] Native Query to DTO 맵핑 (0) | 2022.05.15 |
[Spring] Spring MVC 패턴 & Model2 방식 (0) | 2022.05.01 |