본문 바로가기
기초지식

Spring Security란 ?

by 광진구뚝배기 2023. 4. 26.

시작하는 

 

Spring Security 를 알게 된 배경은 JWT를 사용해보고자 찾다보니 Spring Security 에서 JWT 구현하는 글들이 많이 있어 알게되었다. 그래서 두가지 개념을 먼저 공부하게 되었고, 지금은 프로젝트에 적용하는 중이다. 오늘은 내가 공부한 개념에 대해서 설명하도록 하겠다. JWT에 대한 개념이 궁금하다면 JWT란 ? 게시글을 참고하면 된다.

 

 

 

 

Spring Security 란 ?

 

Spring Security는 스프링 기반의 애플리케이션의 보안(인증, 인가, 권한 등)을 담당하는 스프링 하위 프레임워크다.

 

# 인증 (Authenticate, 누구인지)

   접근하려는 유저가 누구인지 확인하는 절차 

# 인가 (Authorization, 어떤 것을 할 수 있는지)

   인증된 사용자가 요청한 자원에 대해 권한이 있는지를 결정하는 절차

 

 

예를 들자면, 

 

사용자가 tistory에 글을 작성하기 위해 로그인한 것은 인증이다.

이 사용자가 다른 사람의 게시글을 수정 / 삭제 하기 위해 권한을 확인 했지만 다른사람의 게시글이므로 수정 혹은 삭제를 할 수 없는데 이는 인가다.

 

다시 말해 어떠한 사이트에 대해 유효한 사용자인지 확인 하는 것이 인증, 그 인증된 사용자가 해당 기능을 사용할 수 있는지 확인하는 것이 인가다.

 

Spring Security는 이러한 인증 / 인가를 위해 Principal을 아이디로, Credential을 비밀번호로 사용하는 Credential 기반의 인증 방식을 사용한다.

 

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

# Credential (증명서) : 주체가 본인을 인증하기 위해 서버에 제공하는 것. (Resource에 접근하는 대상의 비밀번호)

 

 

 

 

Spring Security  동작 과정

 

출처 : https://doozi0316.tistory.com/entry/Spring-Security-Spring-Security%EC%9D%98-%EA%B0%9C%EB%85%90%EA%B3%BC-%EB%8F%99%EC%9E%91-%EA%B3%BC%EC%A0%95

 

Spring Security를 설정하게 되면 DispatcherServlet에 도달하기 전 서블릿 필터 구현체에 걸리게 된다.

 

 

# 서블릿 필터

 

Spring Security는 FilterChain들을 Servlet Container 기반의 필터위에서 동작하기 위해 중간 연결을 위한 DelegatingFilterProxy 를 사용한다.

 

# Filter   요청과 응답에 대한 정보를 변경할 수 있게 개발자에게 제공하는 Servlet Container

# FilterChain   Filter가 여러개 모여 하나의 체인을 형성하는 것

         ex. 첫번째 필터가 클라이언트가 요청한 정보를 변경하면, 두번째 필터는 첫 Filter에 의해 변경된 요청 정보를 변경한다.

 

DelegatingFilterProxy는 스프링 부트를 쓰면 자동으로 등록이 되는데 그렇지 않다면 AbstractSecurityWebApplicationInitializer 클래스를 상속받아 등록한다.

 

DelegatingFilterProxy 가 등록되고나면 특정 빈(springSecurityFilterChain)으로 필터처리를 위임한다.

 - springSecurityFilterChain 이름으로 등록된 빈인 FilterChainProxy로 요청을 처리한다.

 

 

 

# 인증

 

AuthenticationManager를 사용한다.

 

가장 중요한 인터페이스는 AuthenticationManager이고, 구현체로는 대부분 ProviderManager를 사용한다.

(이 때, AuthenticationManager 인터페이스를 직접 구현할 수도 있다.)

 

ProviderManager는 

 

 

 

# 인가

 

AccessDecisionManager를 사용한다.

 

 

 

 

 

 

Spring Security 모듈

 

# SecutiryContext

 

SecurityContext는 보안 관련 정보를 담고 있는 객체로 현재 인증된 사용자의 세부 정보, 권한 정보, 인증 방식 등을 포함한다.

SecurityContextHolder.getContext() 를 통해 얻을 수 있다.

 

# SecutiryContextHolder

 

SecurityContext를 현재 스레드와 연결시켜주는 역할로 SecurityContext는 SecurityContextHoler 클래스를 통해 사용된다.

- SecurityContextHolder.setContext() : SecurityContext 객체를 현재 스레드에 저장한다.

- SecurityContextHolder.clearContext() : SecurityContext 객체를 제거한다.

 

# Authentication

 

현재 접근하고자 하는 사용자의 정보와 권한을 담는 인터페이스로 Authentication 객체는 SecurityContext에 저장된다.

위에 모듈들을 종합해보면, SecurityContextHoler를 통해 SecurityContext에 접근하고, SecurityContext를 통해 Authentication에 접근할 수 있는 것이다.

 

# UsernamePasswordAuthenticationToken

 

Authentication을 구현(implements)한 AbstractAuthenticationToken의 하위 클레스로, User의 ID가 Principal 역할을 하고, Password가 Credential의 역할을 한다. 

UsernamePasswordAuthenticationToken의 첫 번째 생성자는 인증 전의 객체를 생성하고, 두 번째 생성자는 인증이 완료된 객체를 생성한다.

// 인증 완료 전의 객체 생성
public UsernamePasswordAuthenticationToken(Object principal, Object credentials) {
	super(null);
	this.principal = principal;
	this.credentials = credentials;
	setAuthenticated(false);
}
 
// 인증 완료 후의 객체 생성
public UsernamePasswordAuthenticationToken(Object principal, Object credentials,
		Collection<? extends GrantedAuthority> authorities) {
	super(authorities);
	this.principal = principal;
	this.credentials = credentials;
	super.setAuthenticated(true); // must use super, as we override
}

 

# AuthenticationProvider

 

 

# Authentication Manager

 

# UserDetails

 

principal과 내가 만든 유저 정보 사이를 어댑터 연결하는 것. 

 

# UserDetailsService

 

어디서든 UserDetails에 접근 가능한 클래스

 

# PasswordEncoding

 

# GrantedAuthority

 

 

 

 

 

 

 

 

 

참고자료

 

https://k3068.tistory.com/88

https://doozi0316.tistory.com/entry/Spring-Security-Spring-Securit

https://twer.tistory.com/entry/Security-%EC%8A%A4%ED%94%84%EB%A7%81-%EC%8B%9C%ED%81%90%EB%A6%AC%ED%8B%B0%EC%9D%98-%EC%95%84%ED%82%A4%ED%85%8D%EC%B2%98%EA%B5%AC%EC%A1%B0-%EB%B0%8F-%ED%9D%90%EB%A6%84

반응형

'기초지식' 카테고리의 다른 글

Cookie / Session / Token 이란 ?  (0) 2023.04.23
JWT (Json Web Token) 란 ?  (0) 2023.04.23
Internet Explorer 11 지원 종료, Edge / Window11 전환 방법  (0) 2022.06.10
형상관리 란?  (0) 2022.01.11
Socket (소켓) 이란?  (0) 2022.01.05

댓글