Domain/Spring

Spring Security

by Donghwan 2023. 5. 31.

What is 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 authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements

from. https://spring.io/projects/spring-security

  • Spring Security는 강력하고 사용자화가 가능한 인증과 접근 제어를 하는 Framework입니다.
  • Spring Security는 Spring 기반 Application 보안의 표준입니다.
  • Spring Security는 인증과 인가에 초점을 맞춘 Spring Framework입니다.
  • Spring Security는 쉽게 확장하고 사용자의 요구에 맞출 수 있습니다.

 

Features

  • 인가와 인증에 대한 포괄적이고 확장가능한 지원
  • session fixation, clickjacking, csrf와 같은 공격으로부터의 보호
  • Servlet Api의 통합
  • Spring Web Mvc와의 선택적 통합

 

Authentication & Authorization

Authentication(인증)은 해당 User가 유효한 User인지 확인하는 과정입니다. Id와 Password로 검증할 수도 있고, OAuth와 같이 외부의 도움을 받을 수도 있습니다.

Authorization(인가)은 Authentication(인증)을 거친 유저가 해당 Resource에 접근할 수 있는 권한이 있는지를 확인하는 과정입니다.


Component

Authentication

  • Authentication은 현재 접근하는 주체(Principal)의 정보와 그 주체에 부여된 권한을 담는 Interface로 여기서 주체는 일반적으로 사용자를 의미하지만 실제로 사용자, 시스템, 장치 등 모든것이 될 수 있습니다.
  • Authentication 객체에는 주요한 정보들이 담겨있습니다. 그 중에서 Principal은 사용자를 식별하는데 사용되는 정보를 의미하고 Credentials는 주로 사용자의 비밀번호나 Api 키 등을 의미합니다. Authorities는 사용자에게 부여된 권한을 의미합니다.
  • Authentication 객체는 인증에 성공하면 SecurityContext에 저장되고, SecurityContext는 SecurityContextHolder를 통해서 접근이 가능합니다.
  • Authentication Interface의 구현체 중 일반적으로 사용되는 것은 UsernamePasswrodAuthenticationToken이 있고, 이 클래스는 Username과 Password를 기반으로하는 일반적인 형태의 웹 인증을 나타냅니다.
  • JWT를 사용하는 경우에도 Authentication Interface를 구현한 Token Class를 사용하면 됩니다. 하지만 상황에 따라 직접 구현해야 할 수 있습니다.

Security Context

  • SecurityContext는 Spring Security에서 현재 보안 상황을 나타내는 Interface입니다. Authentication을 보관하는 역할을 하며, 이를 통해 접근하는 Principal와 Authorities에 대한 정보를 Access 할 수 있습니다.
  • Authentication을 보관하는 역할을 하며, SecurityContextHolder를 통해서 현재 SecurityContext를 통해서 Authentication 객체를 꺼내올 수 있습니다.
  • SecurityContext는 Request 처리 중 현재 인증 상태를 유지하는데 사용됩니다. 사용자가 인증하면 Authentication 객체는 SecurityContext에 저장되고 SecurityContext는 SecurityContextHolder에 저장됩니다.
  • 일반적으로 SecurityContext는 요청이 종료될 때 SecurityContextHolder에서 제거되어야 합니다. 이전 요청의 SecurityContext를 재사용하지 않도록 하기 위해서입니다.

SecurityContextHolder

  • 현 시점의 SecurityContext에 접근하는데 사용됩니다. 여기서 SecurityContext는 현재 사용자의 인증 정보를 담고 있는 Authentication 객체를 포함하고 있습니다.
  • ThreadLocal을 기반으로 동작합니다. 이는 각 Thread가 자신만의 SecurityContext를 가질 수 있게하여 각 사용자의 요청을 독립적으로 관리 할 수 있게합니다.
  • 일반적으로 Request가 들어올 때, Filter Chain이 인증 정보를 이용하여 SecurityContext를 생성하고 이를 SecurityContextHolder에 등록합니다. 이렇게 설정된 Context는 이후의 처리 과정에서 현재 사용자에 대한 정보를 가져올 때 사용합니다. 이는 Controller에서 현재 인증된 사용자의 세부 정보에 접근하거나, 권한을 체크하는 등의 보안 관련 작업에 필수적입니다.
  • 일반적으로 SecurityContextHolder에 설정된 SecurityContext는 요청의 끝에서 정리되어야합니다. 이는 다음 요청에서 이전 사용자의 SecurityContext를 사용하지 않도록 보장하는 중요한 단계이고 일반적으로 Spring Security의 Filter가 자동으로 수행합니다.

AuthenticationProvider

  • AuthenticationProvider는 인증 프로세스에서 실제로 사용자의 인정정보를 확인하고 이를 토대로 Authentication 객체를 생성하는 역할을 합니다. Authentication의 구현체를 받아서 이를 검증하고, 검증에 성공하면 새로운 객체를 만들어 반환합니다.
  • 주요 메서드로는 authenticate와 supports가 있습니다. autheticate는 실제 인증 로직을 수행하며, supports는 이 AuthenticationProvider가 특정 Authentication 타입을 지원하는지 확인합니다.
  • Custom한 AuthenticationProvider를 사용하려면 AuthenticationProvider의 구현체를 작성하고, AuthenticationManager에 등록하면 됩니다.

AuthenticationManager

  • AuthenticationManager은 인증 요청을 처리하는 중요한 Interface입니다. 주요 메서드는 authenticate이며 이 메서드는 Authentication 객체를 받아서 인증을 수행하고 인증이 성공하면 새로운 객체를 반환합니다.
  • 인증 절차는 AuthenticationManager에 등록된 AuthenticationProvider를 통해서 처리됩니다. AuthenticationManager는 여러 AuthenticationProvider를 관리하고 인증 요청이 들어오면 적절한 AuthenticationProvider를 사용하여 인증을 수행하게 됩니다.
  • 인증이 성공하면 인증이 성공한 객체를 생성하여 SecurityContext에 저장합니다. 그리고 인증 상태를 유지하기 위해서 세션에 보관하고 인증이 실패한 경우는 AuthenticationException을 발생시킵니다.
  • CustomAuthenticationProvider를 등록하는 방법은 SecurityConfigurerAdapter를 상속해 만든 SecurityConfig에서 할 수 있습니다. AuthenticationManagerBuilder의 authenticationProvider() 메서드를 사용하면 CustomAuthenticationProvider를 등록 할 수 있습니다.

 

FilterChain

  • Spring Security는 Java의 ServletFilter를 기반으로 동작합니다. 이러한 Filter들은 HTTP 요청이 실제로 처리되기 전에 여러 보안검사를 수행하며, 이러한 필터들의 연속적으로 구성하는데 이것이 바로 FilteChain입니다. Filter들은 체인의 순서대로 실행되며, 각 Filter는 요청을 처리하거나 다음 Filter로 전달하거나, 또는 Chain 실행을 중단할 수 있습니다.
  • Filter Chain에는 다음과 같은 필터들이 일반적으로 포함됩니다.
    • SecurityContextPersistenceFilter : Http Request가 시작될 때 SecurityContext를 SecurityContextHolder에 로드하고 요청이 끝날 때 다시 저장하는 역할을 합니다.
    • UsernamePasswrodAuthenticationFilter : Principal과 Credentials를 기반으로 하는 인증을 처리합니다.
    • ExceptionTranslationFilter : 보안 관련 예외를 처리하고 적절한 응답을 보내거나 사용자를 로그인 페이지로 리다이렉트합니다.

 

728x90
반응형

'Domain > Spring' 카테고리의 다른 글

Filter  (0) 2023.01.28
[Spring] Logging과 LoggingFramework  (0) 2022.09.02
[Spring] Web.xml  (0) 2021.11.30
[Spring] JPA 영속성 컨텍스트  (0) 2021.11.18
[Spring] Context ( RootContext, ServletContext )  (0) 2021.11.18

댓글