본문 바로가기
IT Study/SpringBoot

Servlet 인증 구조

by Irwin-Kr 2025. 6. 14.

Spring Security가 Servlet 인증에서 사용하는

주요 구조적인 구성 요소를 설명한다.

 

구체적인 흐름에 대한 설명에 대한 내용을 확인하자.

 

구분 설명
SecurityContextHolder Spring Security가 인증한 사용자의 세부 정보를 저장
SecurityContext 현재 인증된 사용자의 인증과 SecurityContextHolder로 얻을 수 있다.
Authentication 사용자의 인증을 위해 자격증명을 제공하는 Authenticaiton Manager 입력
GrantedAuthority 인증 객체의 권한 부여
AuthenticationManager Spring Security의 FIlter가 인증 수행하는 방법을 정의하는 API
ProviderManager 대부분 AuthenticationManager의 공통 요소
AuthenticationProvider ProviderManager를 사용해 특별한 유형의 인증을 수행
Request Credentials with AuthenticationEntryPoint 단말에서 자격 증명을 요청
AbstractAuthenticationProcessingFilter 인증에 사용되는 기본 Filter. 인증의 높은 수준 흐름을 제공

 

SecurityContextHolder

Spring Security의 인증 모델의 핵심이고, SecurityContext를 포함

 

사용자 인증의 세부 정보를 Spring Security에 저장.

Spring Security는 SecurityContextHolder에 채워지는 방법을 관여하지 않음

값을 포함하고 있으면 현재 인증된 사용자를 사용

 

사용자 인증을 나타내는 간단한 방법은

SecurityContextHolder을 직접 설정

 

[SecurityContextHolder 설정]

// 빈 SecurityContext를 생성
// 여러 스레드의 조건을 피하기 위해 
// SecurityContext.getContext.setAuthentication(authentication); 대신 
// 새 SecurityContext 요소를 생성한다.
SecurityContext context = SecurityContextHolder.createEmptyContext();

// 새 인증 객체 생성
// Spring Security는 SecurityContext에 설정된 인증 구현 유형에 관여하지 않는다.
// 이 부분은 매우 간단하기에 TestingAuthenticationToken을 사용한다.
// 일반적인 시나리오은 UsernamePasswordAuthenticationToken(userDetails, password, authorities);
Authentication authentication = new TestingAuthenticationToken("username", "password", "ROLE_USER");
context.setAuthentication(authentication);

// SecurityContextHolder에 SecurityContext를 설정
// Spring Security는 정보를 인증에 사용한다.
SecurityContextHolder.setContext(context);

 

인증된 객체 정보에 대해 정보를 얻기 위해 SecurityContextHolder에 접근

기본적으로 SecurityContextHolder는 ThreadLocal을 사용해 상세 저장

메소드를 명시적으로 전달하지 않아도 같은 Thread의 메소드로 SecurityContext를 사용

 

TreadLocal을 사용하는 방법은 매우 안전하고, 

객체 요청이 처리된 후 Thread를 비우는것에 주의

Spring Security의 FilterChainProxy는 SecurityContext가 항상 비워짐을 보장

 

일부 응용프로그램은 Thread의 특정 방법으로

(JVM의 모든 Thread가 같은 security context를 사용)

ThreadLocal 사용이 알맞지 않기도 한다.

 

시작 방법으로 SecurityContextHolder를

context 저장 조건 방법을 설정할 수 있다.

 

단일 응용프로그램의 경우 SecurityContextHolder.MODE_GLOBAL 방법을 사용.

 

다른 응용프로그램들은 보안 Thread로 생성된 Thread가 같은 보안 식별임을 가정할 수 있는데

SecurityContextHolder.MODE_INHERITABLETHEADLOCAL을 사용해 달성한다.

 

기본 SecurityContextHolder.MODE_THREADLOCAL에서

상태를 두 가지 방법으로 변경할 수 있다.

 

첫 번째로 시스템 속성 설정.

두 번째는 SecurityContextHolder에서 정적 메소스 호출.

 

대부분의 응용프로그램은 기본값을 변경하지 않아도 되지만

필요하면 Java 문서를 확인해보면 된다.

 

SecurityContext

SecurityContextHolder에서 가져오며, 인증 객체를 포함

 

Authentication(인증)

인증 Interface는 Spring Secuirty에서 두가지 목적을 수행

 

사용자의 인증을 위해 자격증명을 제공하는 Authenticaiton Manager 입력

이 방법에서 isAuthenticated()의 반환은 False

 

인증에 포함되는 사항

객체 : 사용자 식별, Id/Password로 인증할때 사용자의 세부 정보

자격 증명 : 대부분 패스워드로 사용자 인증 후 유출되지 않도록 지운다.

권한 : 사용자에게 허용된 권한으로 역할과 범위를 지정.

'IT Study > SpringBoot' 카테고리의 다른 글

Spring Security 인증 구조 - 3  (2) 2025.07.12
Spring Security 인증 구조 - 2  (0) 2025.06.28
Spring Security(4)  (1) 2025.05.09
Spring Security(3)  (0) 2025.04.26
Spring Security (2)  (0) 2025.04.11