Spring Security 이해하기

qpwoeiru·2024년 3월 30일
0
post-thumbnail

프로젝트에서 남은 할 일들 중 refresh token 도입하기가 있었다. 내가 로그인을 구현한 게 아니었어서 일단 우리 프로젝트에 사용된 Spring Security부터 로직을 이해할 필요가 있었다.


Spring Security

Spring Security란 애플리케이션의 보안에 해당하는 인증, 권한 관리, 데이터 보호 기능을 포함하는 Spring 하위 프레임워크다.

Spring Security를 사용하는 이유?

Spring Security는 Spring 개발 환경에서 보안에 필요한 기능들을 손쉽게 사용할 수 있도록 설계되어 있다. 프레임워크를 사용하지 않고 보안 관련해 코드를 작성할 경우, 인증/인가에 대해 직접 개발하는 것이 쉽지 않지만 Spring Security는 이 인증/인가 역할을 하는 기능을 제공한다.

인증, 권한에 대한 부분을 Filter 흐름에 따라 처리하는데, Spring Security의 구조는 아래처럼 되어있다.

  1. 사용자의 요청이 서버로 들어옴
  2. AuthoticationFilter가 요청을 가로채고 AuthoticationManger로 요청을 위임
  3. AuthoticationManager는 등록된 AuthoticationProvider를 조회하며 인증을 요구
  4. AuthoticationProvider가 실제 데이터를 조회해 UserDetails 결과를 반환
  5. 결과는 SecurityContextHolder에 저장되어 저장된 유저정보를 Spring Controller에서 사용할 수 있게 됨

Spring Security는 인증 절차를 거친 후 인가 절차를 진행하는데, 인가 과정에서 해당 리소스에 대해 접근 권한이 있는지를 확인한다.

  • 인증 (Authentication)
    : 해당 사용자가 본인인지 확인하는 절차
  • 인가 (Authorization)
    : 인증된 사용자가 요청한 리소스에 접근 가능한지 확인하는 절차

위처럼 인증 -> 인가 과정을 거치기 위해 Credential 기반 인증 방식을 사용한다. Credential 기반은 Principal과 Credential가 사용된다.

  • Principal (아이디) : 보호 받는 리소스에 접근하는 대상
  • Credential (비밀번호) : 리소스에 접근하는 대상의 비밀번호

Spring Security 모듈

  • SecurityContextHolder
    보안 주체의 세부 정보를 포함해 응용 프로그램의 현재 보안 컨텍스트에 대한 세부 정보가 저장된다. SecurityContextHolder로 SecurityContext에 접근할 수 있다.

  • SecurityContext
    Authentication을 보관하는 역할로, 여기서 Authentication 객체를 꺼낼 수 있다.

  • Authentication
    Authentication은 현재 접근하는 주체의 정보, 권한이 포함된 인터페이스로, SecurityContextHolder -> SecurityContext -> Authentication 순서로 접근할 수 있다. Authentication 객체에서 Principal, Credential 정보를 가져올 수 있고 isAuthenticated()로 인증 여부를 반환한다.

  • UsernamePasswordAuthenticationToken
    Authentication 인터페이스를 구현한 AbstractAuthenticationToken의 하위 클래스이다. principal과 credentials이 있고 각각 사용자의 ID,PW에 해당한다. 인증 완료 전, 후의 객체를 생성할 수 있는 메서드가 존재한다.

  • AuthenticationProvider
    인증을 처리하는 역할로, 인증 전 Authentication 객체를 받아 인증이 완료된 객체로 반환한다. AuthenticationProvider는 인터페이스이므로 이를 구현해 custom하게 만들어 AuthenticationManager에 등록하면 인증 처리가 가능하다.

  • AuthenticationManager
    AuthenticationProvider를 구현해 AuthenticationManager에 등록하면 인증 성공 시 인증된 Authentication 객체를 제공하고, 이는 SecurityContext에 저장된다.
    AuthenticationManager를 구현한 ProviderManager는 AuthenticationManager를 List로 갖고 있고, ProviderManager는 모든 AuthenticationProvider를 조회하며 authentication을 처리한다.

  • UserDetails
    인증을 성공해 생성된 UserDetails 객체는 Authentication 객체를 구현한 UsernamePasswordAuthenticationToken을 생성하는 데에 사용된다. password, username을 반환하는 메서드가 포함되어 있고, UserDetails 인터페이스를 직접 custom해서 처리할 수도 있다.

  • UserDetailsService
    UserDetails 객체를 반환하는 메서드가 포함되어 있고, 보통은 이 인터페이스를 구현한 클래스 내부에 UserRepository를 주입 받아 DB와 연결하여 처리한다.

  • Password Encoding
    AuthenticationManager.userDetailsService().passwordEncoder()로 password 암호화에 사용될 encoder를 선택할 수 있다.

  • GrantedAuthority
    현재 principal(사용자)이 갖고 있는 권한으로, ROLE_ADMIN, ROLE_USER 등이 있다. UserDetailsService로 불러올 수 있고 특정 리소스에 대해 접근 권한이 있는지 확인하는 데에 사용한다.


참고
https://mangkyu.tistory.com/76

0개의 댓글

관련 채용 정보