경계의 경계

Spring Security란 무엇인가 본문

Spring Security

Spring Security란 무엇인가

gigyesik 2024. 4. 16. 02:09

들어가며

Spring Security는 Java 기반 어플리케이션의 보안을 위한 Spring 하위 프레임워크이다.

Spring Security는 웹 어플리케이션이나 RESTful 서비스에 대한 광범위하고 커스텀 가능한 인증 체계를 제공한다.

Spring Security

Spring Security는 servlet 필터를 사용하여 어플리케이션에 인증(Authentication)과 인가(Auhorization)를 제공한다.

웹 어플리케이션은 인터넷을 통해 누구나 접근할 수 있기 때문에 보안상 취약점과 공격을 받을 위협이 존재한다.

Spring Security는 노출되어 있는 REST 엔드포인트에 보안 설정을 제공한다.

Concepts

Spring Security를 이해하기 위해서는 몇 가지 개념에 대한 이해가 필요하다.

Authentication → 너는 누구인가

‘인증’은 유저의 신원을 파악하는 일이다.

유저가 어플리케이션에 알맞은 계정 정보를 제공하였는지 여부를 판단한다.

Authorization → 너는 무엇을 할 수 있는가

간단한 어플리케이션에서는 authentication만으로 보안 설정을 해결할 수 있다.

하지만 어플리케이션이 복잡해질수록 ‘인가’의 개념이 필요하게 된다.

  • 특정 기능을 사용할 수 있는 유저를 제한하고 싶은 경우
  • 특정 데이터의 변경을 허락하고 싶지 않은 경우
  • 특정 유저에게는 모든 기능을 허용하고 싶은 경우

위와 같은 경우 간단한 인증 절차만으로는 유저의 ‘권한’을 판단할 수 없다.

‘인가’는 특정 유저가 어떤 권한을 가지고 있는지 여부를 판단한다.

Password 저장

계정 정보에 포함되는 비밀번호를 안전하게 관리하는 것은 보안의 최우선 목표이다.

Spring Security의 PasswordEncoder 인터페이스는 아래와 같은 암호화 도구를 통해 복호화 불가능한 패스워드를 생성해준다.

  • Bycrpt
  • Argon2
  • Pbkdf2
  • Scrypt

Servlet 필터

Spring Security는 Java의 Servlet Filter를 이용해 어플리케이션의 보안을 제공한다.

Spring Security 사용의 장점

  • API의 통합적 관리
  • Authentication과 Authorization 전부 통제 가능
  • 세션 고정(Session Fixation)이나 클릭 재킹(Click Jacking)과 같은 보안 공격 방어
  • Spring MVC와 함께 사용 가능
  • Brute Force 공격 방어 가능
  • 이식성이 좋음
  • CSRF공격 방어 가능
  • Java 기반의 설정 제공

Spring Security의 핵심 모듈

Core (spring-security-core)

인증과 접근 제어 클래스와 API. Spring Security를 사용하려면 필수적인 dependency

Web (spring-security-web)

웹 기반 인증에 필요한 필터과 서블릿 기반 접근 제어 환경. Spring MVC와 함께 사용 가능

Config (spring-security-core), LDAP, OAuth2

build.gradle

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-security'
    testImplementation 'org.springframework.security:spring-security-test'
}

필터 추가하기

Java 기반의 어플리케이션에서는 모든 요청에 대해 필요를 사용하게 된다.

org.springframework.web.filter 의 DelegatingFilterProxy 클래스는 Spring Framework가 Spring Security를 사용할 수 있도록 해준다.

DelegatingFilterProxy는 모든 URL로 들어오는 요청을 필터를 사용해 필터링한다.

SecurityContextIntegrationFilter

HTTP 요청을 SecurityContext에 등록한다.

LogoutFilter

유저가 로그아웃할 때마다 SecurityContextHolder를 비운다.

UsernamePasswordAuthenticationFilter

유저가 로그인할 때마다 SecurityContext에 인증을 제공한다.

ExceptionTranslationFilter

SpringSecurity 예외를 HTTP 응답으로 변환한다.

FilterSecurityInterceptor

HTTP 요청을 가로채서 Spring Security에 등록하고, 권한을 확인한다.

SecurityConfig

@Configuration
@EnableWebSecurity
public class SecurityConfig {
	...
}

@EnableWebSecurity 어노테이션을 사용함으로서 Security의 보안 설정을 시작할 수 있다.

로그인 한 유저의 기본 권한은 ‘ROLE_USER’이고, Security의 기본 설정을 통해 basic 방식 기반의 로그인과 로그아웃 기능을 만날 수 있다.

Spring Security의 구조

AuthenticationManager

AuthenticationManager는 ‘인증’을 제공하는 인터페이스이다. 인터페이스를 구현하면 authenticate() 메서드를 오버라이딩해야한다.

public interface AuthenticationManager {
  Authentication authenticate(Authentication authentication)
    throws AuthenticationException;
}

로그인 요청이 인증 필터를 통과하면, AuthenticationManager에 로그인 암호가 제공된다.

암호가 일치하면 authenticate() 메서드는 “True”를 반환하고, 암호가 불일치하거나 암호 일치 여부를 알 수 없는 경우 “AuthenticationException”을 던진다.

AuthenticationProvider

AuthenticationProvider는 AuthenticationManager 인터페이스의 구현체이다.

구조는 비슷하지만, 인증 방식을 결정할 수 있는 support() 메서드를 제공한다.

public interface AuthenticationProvider {
    Authentication authenticate(Authentication authentication)
                   throws AuthenticationException;
    boolean supports(Class<?> authentication);
}

이후 인증 절차가 완료되면 인가 절차가 시작된다.

Resources