[Spring] Spring Security에 대해 (1)
반응형

Spring Security란?

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

Spring Security는 보안과 관련해서 체계적으로 많은 옵션을 제공해주어 개발자는 일일이 보안 관련 로직을 작성하지 않아도 된다는 장점을 제공한다.

Spring security는 Filter 기반으로 동작하기 때문에 Spring MVC 와 분리되어 관리 및 동작한다.

 

인증(Authorizatoin)과 인가(Authentication)

  • 인증 (Authentication): 사용자 신원을 확인하는 행위 -> 회원가입하고 로그인 하는 것
  • 인가 (Authentication): 사용자 권한을 확인하는 행위 -> 웹에서는 주로 역할에 따른 권한 관리 예를들어 카페에 가입하게 되면 등급에 따라 볼 수 있는 게시글이 다르듯이.. 권한이 다르다!

Spring Security는 기본적으로 인증 절차를 거친 후 인가 절차를 진행한다. 어떤 리소스를 요청했을 때 인가 과정에서 해당 리소스에 대한 접근 권한이 있는지 확인을 한다. Spring Security는 이러한 인증과 인가를 위해 Principle을 아이디, Credential를 비밀번호로 사용하는 Credential 기반의 인증 방식을 사용한다.

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

 

 

Spring Security 모듈

SecurityContextHolder

보안 주체의 세부 정보를 포함하여 응용프로그램의 현재 보안 컨텍스트에 대한 세부 정보가 저장된다.

 

SecurityContext

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

 

Authentication

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

 

UsernamePasswordAuthenticationToken

Authentication을 implements한 AbstractAuthenticationToken의 하위 클래스로, User의 ID가 Principal 역할을 하고, Password가 Credential의 역할을 한다. 

 

Authentication Manager

인증에 대한 부분은 SpringSecurity의 AuthenticatonManager를 통해서 처리하게 되는데, 실질적으로는 AuthenticationManager에 등록된 AuthenticationProvider에 의해 처리된다.

 

AuthenticationProvider

AuthenticationProvider에서는 실제 인증에 대한 부분을 처리하는데, 인증 전의 Authentication객체를 받아서 인증이 완료된 객체를 반환하는 역할을 한다.

 

UserDetails

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

 

UserDetailsService

UserDetailsService 인터페이스는 UserDetails 객체를 반환하는 단 하나의 메소드를 가지고 있는데, 일반적으로 이를 구현한 클래스의 내부에 UserRepository를 주입받아 DB와 연결하여 처리한다.

 

 

Spring Security 동작 과정

 

1. 사용자가 로그인 정보를 입력하고 인증 요청(Filter는 Http Request 수신)

  • Spring Security는 필터로 동작을 한다.
  • 요청이 들어오면, 인증과 권한을 위한 필터들을 통하게 된다.
  • 유저가 인증을 요청할때 필터는 인증 메커니즘과 모델을 기반으로한 필터들을 통과한다.
  • 예시
    • HTTP 기본 인증을 요청하면 BasicAuthenticationFilter를 통과한다.
    • HTTP Digest 인증을 요청하면 DigestAuthenticationFilter를 통과한다.
    • 로그인 폼에 의해 요청된 인증은 UseerPasswordAuthenticationFilter를 통과한다.
    • x509 인증을 요청하면 X509AuthenticationFilter를 통과한다.

 

2. 유저 자격을 기반으로 인증토큰(AuthenticationToken) 만들기

  • username과 password를 추출하여 유저 자격을 기반으로 인증 객체를 생성한다.
    -> 대부분의 인증 메커니즘은 username과 password를 기반으로 한다.
  • username과 password는 UsernamePassworkdAutehnticationToekn을 만드는데 사용된다.

 

3. Filter를 통해 AuthenticationToken을 AuthenticationManager에 위임한다

  • UsernamePasswordAuthenticationToken오브젝트가 생성된 후, AuthenticationManager의 인증 메소드를 호출한다.
  • AuthenticationManager는 인터페이스로 정의되어있다.
    - 실제 구현은 ProviderManager에서 한다.

 

4. 실제 인증을 할 AuthenticationProvider에게 UsernamePasswordAuthenticationToken을 다시 전달한다.

 

5. DB에서 사용자 인증 정보를 가져올 UserDetailsService 객체에게 사용자 아이디를 넘겨준다.

  • UserDetailsService는 username 기반의 userDetails를 검색하는 비즈니스 로직을 정의해두는 곳..
    -> AuthenticationProvider에서 제공하고 있는 DaoAuthenticationProvider를 사용한다고 함.

 

6. UserDetails를 이용해서 User객체에 대한 정보를 검색한다.

  • UserDetailsService는 인터페이스이므로, 우리가 인증하고자하는 비즈니스로직을 정의한 serivce레이어에서 구현을 실행한다.

 

7. User객체의 정보들을 UserDetails가 UserDetailsService(LoginService)에 전달한다

  • 전달된 User 객체의 정보와 사용자가 요청한 인증 정보(username, password)를 확인하는 로직을 LoginService에 구현한다.

 

8. 인증 객체 또는 AuthenticationException 리턴

  • 유저의 인증이 성공하면, 전체 인증정보를 리턴한다.
  • 인증에 실패하면 AuthenticationException을 던진다.

 

9. AuthenticationManager는 완전한 인증(Fully Populated Authentication)객체를 Authentication Filter에 반환한다.

 

10. SecurityContext에 인증 객체를 설정한다.

  • AuthenticationFilter는 인증 객체를 SecurityContext에 저장한다.

 

 

 

 

 

Spring Security의 기초적 개념들을 다뤄보았다.. 참고사이트들은 아래에..

Reference : 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, https://mangkyu.tistory.com

반응형