본문 바로가기

Programming/[Spring]

[Spring] Spring Security

1. Spring Security의 의의

- Spring Security는 Spring 기반의 application의 보안(인증과 권한, 인가 등)을 담당하는 Spring 하위 Framework

- '인증'과 '권한'에 대한 부분을 Filter 흐름에 따라 처리

- Filter는 Dispatcher Servlet으로 가기 전에 적용되므로 가장 먼저 URL 요청을 받지만, Interceptor는 Disapatcher와 Controller 사이에 위치한다는 점에서 적용 시기의 차이가 있다.

- 보안과 관련하여 체계적으로 많은 옵션 제공

 

2. Spring Security Architecture

3. Authorization (인증)과 Authentication (인가)

 - Authorization (인증) : 해당 사용자가 본인이 맞는지를 확인하는 절차

 - Authentication (인가) : 인증된 사용자가 요청한 자원에 접근 가능한지를 결정하는 절차

Spring Security는 기본적으로 인증 절차를 거친 후에 인가 절차를 진행
인가 과정에서 해당 리소스에 대한 접근 권한이 있는지 확인
Principal을 아이디, Credential을 비밀번호로 사용하는 Credential 기반의 인증 방식 사용

 - Principal (접근 주체) :  보호 받는 Resource에 접근하는 대상

 - Credential (비밀번호) : Resource에 접근하는 대상의 비밀번호

 

4. Spring Security 주요 모듈

 1 ) SecurityContextHolder

 - SecurityContextHolder는 보안 주체의 세부 정보를 포함하여 응용프로그램의 현재 보안 컨텍스트에 대한 세부 정보가 저장

 2 ) SecurityContext

 - Authentication을 보관하는 역할, SecurityContext를 통해 Authentication 객체를 사용 가능

 3 ) Authentication 

 - Authentication은 현재 접근하는 주체의 정보와 권한을 담는 인터페이스.  Authentication 객체는 Security Context에 저장, SecurityContextHolder를 통해 SecurityContext에 접근, SecurityContext를 통해 Authentication에 접근 가능

 4 ) UsernamePasswordAuthenticationToken

 - UsernamePasswordAuthenticationToken은 Authentication을 implements한 AbstractAithenticationToken의 하위 클래스. User의 Id가 Principal 역할, Password가 Credential 역할. UsernamePasswordAuthenticationToken의 첫번째 생성자는 인증 전의 객체를 생성, 두번째 생성자는 인증이 완료된 객체 생성

 5 ) AuthenticationProvider

 - AuthenticationProvider에서는 실제 인증에 대한 부분을 처리. 인증 전의 Authentication객체를 받아서 인증이 완료된 객체를 반환하는 역할.

 6 ) Authentication Manager

 - 인증에 대한 부분은 AuthenticationManager를 통해 처리. 실질적으로는 AuthenticationManager에 등록된 AuthenticationProvider에 의해 처리. 인증이 성공하면 2번째 생성자를 이용해 인증이 성공한(isAuthenticated=true) 객체를 생성하여 Security Context에 저장한다. 그리고 인증 상태를 유지하기 위해 세션에 보관하며, 인증이 실패한 경우에는 AuthenticationException를 발생

 7 ) UserDetails

 - 인증에 성공하여 생성된 UserDetails 객체는 Authentication객체를 구현한 UsernamePasswordAuthenticationToken을 생성하기 위해 사용

 8 ) UserDetailsService

 - UserDetailsService 인터페이스는 UserDetails 객체를 반환하는 단 하나의 메소드를 가지고 있는데, 일반적으로 이를 구현한 클래스의 내부에 UserRepository를 주입받아 DB와 연결하여 처리

 9 ) PasswordEncoding

 - AuthenticationManagerBuilder.userDetailsService().passwordEncoder()를 통해 패스워드 암호화에 사용될 PasswordEncoder 구현체를 지정 가능

 10 ) GrantedAuthority

 - GrantAuthority는 현재 사용자(principal)가 가지고 있는 권한을 의미한다. ROLE_ADMIN나 ROLE_USER와 같이 ROLE_*의 형태로 사용하며, 보통 "roles" 이라고 한다. GrantedAuthority 객체는 UserDetailsService에 의해 불러올 수 있고, 특정 자원에 대한 권한이 있는지를 검사하여 접근 허용 여부를 결정한다.

 

 

'Programming > [Spring]' 카테고리의 다른 글

[Spring Boot] Spring Initializr Option  (1) 2024.01.31
[Spring] 자주 나오는 면접 질문  (0) 2021.12.21
[Spring] Spring MVC  (0) 2019.06.12
[Spring] Maven  (0) 2019.06.12
[Spring] Springboot  (0) 2019.06.05