DevBoi

[Spring Security] 스프링 시큐리티 기초 본문

Develop

[Spring Security] 스프링 시큐리티 기초

HiSmith 2021. 12. 5. 12:57
반응형

 

1. 초기 request가 오면, applicationFilter가 받는다. 이후 DelegatingFilterProxyRegistrationBean라는 (Application FIlter에 있는 ) 녀석이 DelegatingFilterProxy를 빈으로 등록해준다.

해당 빈의 클래스로 등록이 되어있는, springSecurityFilterChain 이 녀석이 그 이후 필터로 동작을 하게 된다.

이 필터 체인은 여러가지의 객체를 리스트로 가지고있고, 해당 리스트의 아이템값을 돌면서 필터링한다.

 

 

AuthenticationFilter 리스트 (위에서 아래 순서로 동작한다.

  • WebAsyncManagerIntegrationFilter
    • SpringSecurityContextHolder는 ThreadLocal기반(하나의 쓰레드에서 SecurityContext 공유하는 방식)으로 동작하는데, 비동기(Async)와 관련된 기능을 쓸 때에도 SecurityContext를 사용할 수 있도록 만들어주는 필터
  • SecurityContextPersistenceFilter
    • SecurityContext가 없으면 만들어주는 필터
    • SecurityContext는 Authentication 객체를 보관하는 보관 인터페이스다. (구현체가 있어야겠지)
  • HeaderWriterFilter
    • 응답(Response)에 Security와 관련된 헤더 값을 설정해주는 필터
  • CsrfFilter
    • CSRF 공격을 방어하는 필터
  • LogoutFilter
    • 로그아웃 요청을 처리하는 필터
    • DefaultLogoutPageGeneratingFilter가 로그아웃 기본 페이지를 생성함
  • UsernamePasswordAuthenticationFilter → username, password를 쓰는 form기반 인증을 처리하는 필터.
    • AuthenticationManager를 통한 인증 실행
    • 성공하면, Authentication 객체를 SecurityContext에 저장 후 AuthenticationSuccessHandler 실행
    • 실패하면 AuthenticationFailureHandler 실행
  • RequestCacheAwareFilter
    • 인증 후, 원래 Request 정보로 재구성하는 필터
  • SecurityContextHolderAwareRequestFilter
  • AnonymousAuthenticationFilter
    • 이 필터에 올 때까지 앞에서 사용자 정보가 인증되지 않았다면, 이 요청은 익명의 사용자가 보낸 것으로 판단하고 처리한다. (Authentication 객체를 새로 생성함(AnonymousAuthenticationToken))
  • SessionManagementFilter
    • 세션 변조 공격 방지 (SessionId를 계속 다르게 변경해서 클라이언트에 내려준다)
    • 유효하지 않은 세션으로 접근했을 때 URL 핸들링
    • 하나의 세션 아이디로 접속하는 최대 세션 수(동시 접속) 설정
    • 세션 생성 전략 설정
  • ExceptionTranslationFilter
    • 앞선 필터 처리 과정에서 인증 예외(AuthenticationException) 또는 인가 예외(AccessDeniedException)가 발생한 경우, 해당 예외를 캐치하여 처리하는 필터 (모든 예외를 다 이 필터에서 처리하는 건 아님)
  • FilterSecurityInterceptor
    • 인가(Authorization)를 결정하는 AccessDecisionManager에게 접근 권한이 있는지 확인하고 처리하는 필터

 

springSecurityFilterChain 의 필터가 갖고있는 필터중

 

UsernamePasswordAuthenticationFilter - form의 id,pw를 토대로 인증을 실시하는 필터
OAuth2ClientAuthenticationProcessingFilter - OAuth2 토대로 인증을 실시하는 필터

여러개의 필터를 거치면서, 어떤 필터에서 인증이 완료되면, 인증이 완료된 요청이 된다.

 

UsernamePasswordAuthenticationFilter
해당 필터는, form의 id,pw형태로 인증을 실시하는 필터이다.

해당 필터에서는 UsernamePasswordAuthenticationToken를 만들어서 return 해주는 역할을 한다.

정확히 해당 필터에서는 해당 토큰을 가지고 인증작업을 하지는 않는다.

해당 필터가 AuthenticationManager 해당 매니저에게 인증을 해달라고 던져주고, 해당 매니저가 인증작업을 수행한다

 

ProviderManager는 여러 AuthenticationProvider들을 순회하면서 UsernamePasswordAuthenticationToken을 처리해줄 AuthenticationProvider를 찾고 인증해준다.

 

form에 대한 인증 처리 관련된 내용이다.

더 딥한 건, oAuth2 인증과정을 보면서 진행해보도록 하자

 

 

 

 

반응형