Spring Security

김수혁·2023년 12월 29일

Spring

목록 보기
2/4

Spring Security


Spring에서 제공해주는 인증(Authentication)과 인가(Authorization)에 대한 처리를 위임하는 별도의 프레임워크이다.
Spring Security는 인증과 인가에 대한 부분을 Filter의 흐름에 따라 처리한다.
Spring Security는 보안과 관련하여 체계적으로 많은 옵션을 제공해주기 때문에 개발자 입장에서는 일일이 보안 관련 로직을 작성하지 않아도 된다는 장점이 존재한다.

인증

  • 해당 사용자가 본인이 맞는지를 확인하는 절차

인가

  • 인증된 사용자가 요청된 자원에 접근가능한가를 결정하는 절차

Spring Security 구조

Spring Security 동작 과정


1. Http Request 수신

클라이언트로부터 로그인 정보와 함께 인증 요청을 하면 AuthenticationFilter에서 인증 및 권한 부여의 목적으로 일련의 필터를 거치게 된다.

로그인이 이미 되어있는 상태
이미 Http Session에 Authentication 객체가 저장되어 있어 Authentication Manager에서 authentication()메서드를 호출하지 않고 Security Context Holder에서 Authentication 객체를 가져와 인증 후 곧바로 클라이언트의 요청을 수행

시큐리티에서 제공하는 유저 인증 관련 필터

  • UsernamePasswordAuthenticationFilter : 사용자의 이름과 비밀번호를 사용한 기본 폼 기반 인증을 처리하는 필터, 세션에 사용자 정보 저장
  • JwtAuthenticationFilter : JWT(Jason Web Token)를 사용한 인증을 처리하는 필터. 클라이언트가 JWT 토큰을 제공하면 필터가 해당 토큰으로 사용자를 인증

2. 사용자 자격증명을 기반으로 한 Authentication Token 생성

유저의 인증 요청에서 Authentication Filter를 통해 유저의 이름과 패스워드를 추출한다.
추출된 정보를 기반으로 인증 토큰(UsernamePasswordAuthentication Token)이 생성된다.
생성된 토큰은 Authentication Manager의 authentication() 함수를 통해 Provider Manager로 넘어간다.

3. Authentication Manager에게 인증 토큰 위임

UsernamePasswordAuthentication Token 객체 생성 후 Authentication Manager의 인증 메서드를 호출하는데 사용된다.

4. Autehntication Provider 목록으로 사용자 인증 처리

Authentication Provider는 특정 인증을 수행하며 사용자의 자격 증명을 확인하고 사용자를 인증한다. 인증에 성공하면 인증된 Authentication 객체를 반환한다.

DaoAuthentication Provider
유저 이름과 비밀번호를 사용한 인증 처리

JwtAuthentication Provider
JWT를 사용한 인증 처리

5. UserDetailService에 Authentication 넘기기

UserDetailService에 Authentication 객체를 넘겨준다.

6. UserDetailsService로부터 UserDetails 검색

UserDetailService는 DB에서 사용자의 정보를 토대로 검색하고 해당 데이터를 UserDetails 객체에 매핑시켜 반환한다.

7. Authentication Providers에게 UserDetails 객체 전달

Authentication Providers는 UserDetail 객체를 받아 유저 정보를 비교한다.

8. 성공 시 인증개체 반환 OR 실패시 인증 예외 발생

성공 시 AuthenticationSuccessHAndle을 실패 시 AuthenticationFailtureHandle을 실행한다.
인증이 완료되면 Principal, Credentials, Authorities, Authenticated를 설정한다.

9. 인증 완료

Authentication Manager는 Authentication 객체를 관련 Authentication Filter로 반환한다.

10. SecurityContext에 Authentication 객체 저장

SecurityContextHolder는 세션 영역에 있는 Security Context에 Authentication 객체를 저장한다.

profile
안녕안녕안녕

0개의 댓글