스프링 시큐리티(Spring Security)란?
스프링 시큐리티 (Spring Security)는 스프링 기반 어플리케이션의 보안(인증과 권한, 인가)을 담당하는 스프링 하위 프레임워크이다. 스프링 시큐리티는 '인증'과 '권한'에 대한 부분을 Filter 흐름에 따라 처리하고 있다. 스프링 시큐리티를 사용하면 보안과 관련해서 체계적으로 많은 옵션들을 제공해주기 때문에 개발자의 입장에서는 하나하나 보안 관련 로직을 작성하지 않아도 된다는 장점이 있다.
Spring Security 관련 용어
- 인증 Authentication : 해당 사용자가 본인이 맞는지를 확인하는 절차
- 인가 Authorization : 인증된 사용자가 요청한 자원에 접근 가능한지를 결정하는 절차
- 접근 주체 Principal : 보호받는 Resource에 접근하는 대상
- 비밀번호 Credential : Resource에 접근하는 대상의 비밀번호
Spring Security 동작 과정

1. 사용자가 로그인 정보와 함께 인증 요청을 한다.(Http Request)
2. AuthenticationFilter가 요청을 가로채고, 가로챈 정보를 통해 UsernamePasswordAuthenticationToken의 인증용 객체를 생성한다.
3. AuthenticationManager의 구현체인 ProviderManager에게 생성한 UsernamePasswordToken 객체를 전달한다.
4. AuthenticationManager는 등록된 AuthenticationProvider(들)을 조회하여 인증을 요구한다.
5. 실제 DB에서 사용자 인증정보를 가져오는 UserDetailsService에 사용자 정보를 넘겨준다.
6. 넘겨받은 사용자 정보를 통해 DB에서 찾은 사용자 정보인 UserDetails 객체를 만든다.
7. AuthenticationProvider(들)은 UserDetails를 넘겨받고 사용자 정보를 비교한다.
8. 인증이 완료되면 권한 등의 사용자 정보를 담은 Authentication 객체를 반환한다.
9. 다시 최초의 AuthenticationFilter에 Authentication 객체가 반환된다.
10. Authenticaton 객체를 SecurityContext에 저장한다.
최종적으로 SecurityContextHolder는 세션 영역에 있는 SecurityContext에 Authentication 객체를 저장한다.
사용자 정보를 저장한다는 것은 Spring Security가 전통적인 세션-쿠키 기반의 인증 방식을 사용한다는 것을 의미한다.
Spring Security 주요 모듈, 필터
Filter: 앱의 제일 앞단에서 HTTP요청을 가로채는데 사용되는 객체
Authentication
- 현재 접근하는 주체의 정보와 권한을 담는 인터페이스이다. Authentication 객체는 SecurityContext에 저장되며, SecurityContextHolder를 통해 SecurityContext에 접근하고, SecurityContext를 통해 Autentication에 접근할 수 있다.
UsernamePasswordAuthenticationToken
- AbstractAuthenticationToken의 하위 클래스로, User의 ID가 Principal 역할을 하고, Password가 Credential의 역할을 한다. UsernamePasswordAuthenticationToken의 첫 번째 생성자는 인증 전의 객체를 생성하고, 두번째는 인증이 완료된 객체를 생성한다.
- AuthenticationFilter에 의해 생성된 객체이며, 로그인을 시도한 아이디와 비밀번호 데이터를 담고 있음
AuthenticationManager
- 인증에 대한 부분은 AuthenticationManager를 통해서 처리하게 되는데, 실질적으로는 AuthenticationManager에 등록된 AuthenticationProvider에 의해 처리된다. 인증에 성공하면 두 번째 생성자를 이용해 객체를 생성하여 SecurityContext에 저장한다.
- authenticate라는 인증 메소드를 제공하는 인터페이스
AuthenticationProvider
- AuthenticationProvider에서는 실제 인증에 대한 부분을 처리하는데, 인증 전의 Authentication 객체를 받아서 인증이 완료된 객체를 반환하는 역할을 한다. 아래와 같은 인터페이스를 구현해 Custom한 AuthenticationProvider를 작성하고 AuthenticationManager에 등록하면 된다.
ProviderManager
- AuthenticationManager를 구현하는 객체로 authenticate메소드를 오버라이딩한다. authenticate메소드 안에서 멤버로 갖고있는 Provider를 순회하며 UsernamePasswordAuthenticationToken를 처리할 수 있는 Provider를 찾아서 요청 위임
UserDetailsService
- 인증에 성공하여 생성된 UserDetails 객체는 Authentication객체를 구현한 UsernamePasswordAuthenticationToken을 생성하기 위해 사용된다. UserDetails를 implements하여 처리할 수 있다. 즉, UserDetailsService 인터페이스는 DB에서 유저 정보를 가져오는 역할을 한다.
UserDetails
- Spring Security에서 관리하는 사용자 정보
- 인증에 성공하여 생성된 UserDetails 객체는 Authentication객체를 구현한 UsernamePasswordAuthenticationToken을 생성하기 위해 사용된다. UserDetails를 implements하여 처리할 수 있다.
SecurityContextHolder
- SecurityContext를 저장하는 객체이다. Securitycontextholder.getcontext().getauthentication()를 통해 SecurityContext안에 저장된 authentication객체를 꺼내올 수 있다.
SecurityContext
- 현재 인증 된 사용자의 정보(Authentication객체)를 저장하는데 사용된다. SecurityContext를 통해 Authentication을 저장하거나 꺼내올 수 있다.
GrantedAuthority
- GrantedAuthority는 현재 사용자(Principal)가 가지고 있는 권한을 의미하며, ROLE_ADMIN이나 ROLE_USER와 같이 ROLE_*의 형태로 사용한다. GrantedAuthority 객체는 UserDetailsService에 의해 불러올 수 있고, 특정 자원에 대한 권한이 있는지를 검사하여 접근 허용 여부를 결정한다.
대략적인 로그인 과정
- 로그인 시도 -> username, password 정보를 HTTP body로 전달
- 인증 관리 -> UserDetailsService에게 username을 전달하고 회원 상세정보를 요청
- 회원 DB에서 회원 조회 -> 조회된 정보를 UserDetails로 변환
- 인증 관리자가 인증 처리 -> UserDetailsService가 전달해준 UserDetails의 정보와
클라이언트가 시도한 username,password 일치 여부 확인 - UserDetails의 password는 암호문이기에 클라이언트가 보낸 password를 암호화 하여 비교
Reference
Spring Security
Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications. Spring Security is a framework that focuses on providing both authentication and authoriz
spring.io
'Spring' 카테고리의 다른 글
| [Spring] UserDetails, UserDetailsService (2) | 2023.12.06 |
|---|---|
| [Spring] 싱글톤 방식의 주의점 (0) | 2023.06.14 |
| [Spring] 스프링이란? (0) | 2023.06.01 |
| [Spring] MultipartFile 인터페이스 (0) | 2023.04.19 |
| [Spring] 스프링이란? (0) | 2023.04.12 |