Spring Security 기초

단비·2023년 5월 8일
0

학습

목록 보기
37/66

Spring Security란

  • Spring 기반의 애플리케이션의 보안(인증과 권한, 인가 등)을 담당하는 스프링 하위 프레임워크
  • Principal을 아이디로, Credential을 비밀번호로 사용하는 Credential 기반의 인증 방식을 사용

Principal(접근 주체): 보호받는 Resource에 접근하는 대상
Credential(비밀번호): Resource에 접근하는 대상의 비밀번호


인증과 인가란

  • 인증(Authentication)
    • 해당 사용자가 본인이 맞는지 확인하는 절차
  • 인가(Authorization)
    • 인증된 사용자가 요청한 자원에 접근 가능한지 결정하는 절차




Spring Security의 주요 모듈

  • SecurityContextHolder

    • 보안 주체의 세부 정보를 포함하여 응용프래그램의 현재 보안 컨텍스트에 대한 세부 정보가 저장됨
    • SecurityContextHolder.MODE_INHERITABLETHREADLOCAL 방법과
      SecurityContextHolder.MODE_THREADLOCAL 방법을 제공
  • SecurityContext

    • Authentication을 보관하는 역할을 하며,
      SecurityContext를 통해 Authentication 객체를 꺼내올 수 있음
  • Authentication

    • 현재 접근하는 주체의 정보와 권한을 담는 인터페이스
    • Authentication 객체는 Security Context에 저장되며,
      SecurityContextHolder를 통해 SecurityContext에 접근하고,
      SecurityContext를 통해 Authentication에 접근할 수 있음
  • UsernamePasswordAuthenticationToken

    • Authentication을 implements한 AbstractAuthenticationToken의 하위 클래스
    • User의 ID가 Principal 역할을 하고, Password가 Credential의 역할을 함
  • AuthenticationProvider

    • 실제 인증에 대한 부분을 처리하는데, 인증 전의 Authentication객체를 받아서 인증이 완료된 객체를 반환
    • 아래와 같은 AuthenticationProvider 인터페이스를 구현해서 Custom한 AuthenticationProvider을 작성해서 AuthenticationManager에 등록하면 된
  • Authentication Manager

    • 인증에 대한 부분 처리
      (실질적으로는 AuthenticationManager에 등록된 AuthenticationProvider에 의해 처리)
    • 인증이 성공하면 2번째 생성자를 이용해 인증이 성공한(isAuthenticated=true) 객체를 생성하여 Security Context에 저장
      그리고 인증 상태를 유지하기 위해 세션에 보관하며, 인증이 실패한 경우에는 AuthenticationException를 발생 시킴
  • UserDetails

    • 인증에 성공하여 생성된 UserDetails 객체는 Authentication객체를 구현한 UsernamePasswordAuthenticationToken을 생성하기 위해 사용
  • UserDetailsService

    • UserDetails 객체를 반환하는 단 하나의 메소드를 가지고 있음
    • 일반적으로 이를 구현한 클래스의 내부에 UserRepository를 주입받아 DB와 연결하여 처리
  • Password Encoding

    • AuthenticationManagerBuilder.userDetailsService().passwordEncoder() 를 통해 패스워드 암호화에 사용될 PasswordEncoder 구현체를 지정할 수 있음
  • GrantedAuthority

    • 현재 사용자(principal)가 가지고 있는 권한
    • ROLE_ADMIN나 ROLE_USER와 같이 ROLE_*의 형태로 사용하며, 보통 "roles" 라고 함
    • UserDetailsService에 의해 불러올 수 있고, 특정 자원에 대한 권한이 있는지를 검사하여 접근 허용 여부를 결정

profile
tistory로 이전! https://sweet-rain-kim.tistory.com/

0개의 댓글