티스토리 뷰

인증

  • Username과 Password 인증
    • Session 관리
    • 토큰 관리 (Session less) : 서버의 scale-out 을 통해 여러대의 서버로 서비스 하는 경우가 많기 때문에 서버끼리 서로 세션을 공유 및 동기화해야하기 때문에 토큰을 사용

권한

  • @Secured 을 활용한 권한체크
  • @PrePostAuthorize 을 활용한 권한체크
  • AOP 를 활용한 권한체크

서블릿 컨테이너 동작

  1. request 가 threadlocal 로 실행되어 들어옴
  2. 필터를 거친 다음 DispatcherServlet은 request의 url에 따라 분기되고 실행될 메서드를 찾아 request, response 를 넘김

Spring Security 정책 적용 방식

  • 상황에 따라 다른 Security Filter 가 실행될 수 있기 때문에 필터 사이에 DelegatingFilterProxy 를 만든 뒤 메인 필터 체인에 넣고 그 아래에 SecurityFilterChain 그룹을 등록한다
  • 필터 프록시는 해당 요청을 보고 어떤 필터를 거칠지 결정하는 역할을 한다

Security Filter

  • 각 필터는 서로 다른 관심사를 갖는다

Spring Security Filter 종류

  1. HeaderWriterFilter : Http 헤더 검사
  2. CorsFilter : 허가된 사이트나 클라이언트의 요청인지 검사
  3. CsrfFilter : 내가 요청한 리소스가 맞는지 검사
  4. LogoutFilter : 로그아웃 검사
  5. UsernamePasswordAuthenticationFilter : form based authentication 방식으로 인증을 진행할 때 아이디, 패스워드 데이터를 파싱해서 인증 요청을 위임하는 필터
  6. 아이디와 패스워드 데이터를 가져온 뒤 토큰 생성 후 인증을 다른곳에 위임
  7. BearerTokenAuthenticationFilter : Authentication 헤더에 Bearer 토큰이 오면 인증 처리
  8. BasicAuthenticationFilter : Authentication 헤더에 Basic 토큰이 오면 인증 처리
  9. RequestCacheAwareFilter : 방금 요청한 request 이력이 다음에 필요하다면 캐시에 저장하는 필요
  10. RememberMeAuthenticationFilter : 인증이 안 된 경우라면 RememberMe 쿠키를 검사해서 인증. 세션은 만료되었지만 브라우저가 가지고있는 쿠키로 인증
  11. AnonymouseAuthenticationFilter : 인증이 안된 사용자의 헤더를 Anonymouse 처리

Spring Security 에서 로그인

  • Spring Security 에서 로그인을 했다는 것은 Spring Security Context 안에 authenticated 가 true 인 Authentication 객체**를 가지고 있는 상태

→ 이 때 Authentication 은 AnonymousAuthenticationToken 이 아니어야함

Authentication 기본 구조

  • Security Filter 중 일부 필터는 인증 제공
  • 인증 필터는 AuthenticationManager 통해 Authentication 인증 후 결과를 인증 결과의 보관소라고 할 수 있는 SecurityContextHolder 에 넣어준다
  • AuthenticationProvider는 인증을 제공하는 역할을 하고 1개 이상 존재할 수 있음
  • AuthenticationManager 는 인증 관리자처럼 AuthenticationProvider를 관리

Authentication(인터페이스 → 구현체는 AuthenticationToken) 제공 필터

Form login 인증 (아이디 패스워드를 이용한 로그인)
  1. ID/PW 로 로그인을 하면 UsernamePasswordAuthenticationFilter 를 통해 form login 이 이루어진다.
  2. 세션은 로그인을 하고 해당 Authentication 을 담고 있다가 이후 로그인시 SecurityContextPersistenceFilter를 통해 Security Context Holder 에 Authentication 을 넣어줌
  3. 세션이 만료가 되어도 remember-me 가 체크가 되어있다면 remember-me 쿠키를 가지고 있기 때문에 RememberMeAuthenticationFilter 를 거칠 때 Authentication 이 자동으로 삽입
  4. 만약 인증이 되지 않은 상황에서 AnonymousAuthenticationFilter 에 사용자의 request 가 들어오면 AnonymousAuthenticationToken 발행

→ 내 프로젝트에서는 form login 사용.. 로그인 페이지 지정을 해야했기 때문..

HttpBasic 인증
  1. Authrization 헤더에 username, password를 BASE64 로 인코딩해서 보낸다
  2. BasicAuthenticationFilter 는 해당 request 를 받고 인증을 해준 뒤 바로 요청하는 페이지로 응답

→ SPA 나 모바일의 경우 HttpBasic 을 사용 / 세션이 있는 경우 사용

JWT 인증

세션이 없는 경우 토큰에 많은 정보를 담아야 하기 때문에 세션이 없는 경우는 BearerTokenAuthenticationFilter 를 통해 인증 처리를 한다.

소셜 인증

소셜 로그인을 위해 OAuth2LoginAuthenticationFilter, OAuth2LoginAuthenticationToken, OAuth2AuthenticationToken 을 사용

OpenID 인증

OpenIDAuthenticationFilter 을 통해 인증

SAML2 인증

Saml2WebSsoAuthenticationFilter 을 통해 인증

Custom 인증

custom filter 를 생성해서 인증 과정 처리 가능

Authentication (인증토큰) 정보

  1. Set<GrantedAuthority> authority : 인증된 권한 정보, 1개 이상의
  2. principal : 인증 대상에 관한 정보, UserDetails 객체
  3. credentials : 인증 확인을 위한 정보, 비밀번호 → 보안때문에 삭제
  4. details : 그 밖에 필요한 정보, IP, 세션정보 등
  5. boolean authenticated : 인증 되었는지 체크, AnonymousAuthenticationToken 의 경우 해당 필드가 false

'패캠강의정리' 카테고리의 다른 글

[Spring Security ] Form login  (0) 2022.08.06
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함