dev._.note

[SPRING] Spring Security Filter 본문

Dev/SPRING

[SPRING] Spring Security Filter

Laena 2023. 3. 3. 15:25

Spring Security?

Spring Security는 스프링 기반의 어플리케이션 보안을 담당하는 프레임워크.

Spring Security를 사용하면 사용자 인증, 권한, 보안처리를 간단하지만 강력하게 구현 할 수 있다.

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

 

  • 인증(Authentication) 접근한 유저를 식별하고, 애플리케이션에 접근할 수 있는지 검사하는 것
    • 접근 주체(Principal) 보안 시스템이 작동되고 있는 애플리케이션에 접근하는 유저
    • Credentials 특정리소스에 접근하려는 사용자가 인증을 위해 제공하는 비밀번호
  • 인가(Authorization) 인증된 유저가 애플리케이션의 기능을 이용할 수 있는지 검사하는 것.
    • 권한 Authorities 인증된 유저가 가진 권한 목록

Spring Security Filter Chain

@web.xml

  1. DelegatingFilterProxyRegistrationBean이 DelegatingFilterProxy필터클래스를springSecurityFilterChain이라는 이름의 Filter로 등록
    • legacy project에서는 web.xml에서 DelegatingFilterProxy필터 등록시 application-context에 빈으로 등록됨.
  2. Servler이전 FilterChain실행중, springSecurityFilterChain 에 도달하면, DelegatingFilterProxy클래스는 FilterChainProxy 클래스에게 처리를 위임.
    • FilterChainProxy는 AuthenticationFilter 들을 리스트로 보관하고 있는 클래스
    • FilterChainProxy의 AuthenticationFilter들을 순서대로 실행.
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>
</filter-mapping>


Filter Chain 상세

The Security Filter Chain

7.3 Filter Ordering

  1. ChannelProcessingFilter because it might need to redirect to a different protocol
  2. *SecurityContextPersistenceFilter, so a SecurityContext can be set up in the SecurityContextHolder at the beginning of a web request, and any changes to the SecurityContext can be copied to the HttpSession when the web request ends (ready for use with the next web request)
    • Authentication객체를 보관하는 SecurityContext객체를 생성해주는 필터
  3. ConcurrentSessionFilter, because it uses the SecurityContextHolder functionality but needs to update the SessionRegistry to reflect ongoing requests from the principal
  4. Authentication processing mechanisms - SecurityContextHolder can be modified to contain a valid Authentication request token.
    • *UsernamePasswordAuthenticationFilter AbstractAuthenticationProcessingFilter 를 상속.
    • CasAuthenticationFilter
    • BasicAuthenticationFilter
    • etc
  5. SecurityContextHolderAwareRequestFilter, if you are using it to install a Spring Security aware HttpServletRequestWrapper into your servlet container
    • HttpServlet 인증 방식에 따라 ServletRequest객체를 재구성하는 필터
  6. RememberMeAuthenticationFilter, so that if no earlier authentication processing mechanism updated the SecurityContextHolder, and the request presents a cookie that enables remember-me services to take place, a suitable remembered Authentication object will be put there
  7. AnonymousAuthenticationFilter, so that if no earlier authentication processing mechanism updated the SecurityContextHolder, an anonymous Authentication object will be put there
    • 이 필터에 올 때까지 인증 객체가 생성되지 않으면 익명의 사용자가 보낸 것으로 판단하고 처리한다. (Authentication 객체를 새로 생성)
  8. *ExceptionTranslationFilter, to catch any Spring Security exceptions so that either an HTTP error response can be returned or an appropriate AuthenticationEntryPoint can be launched
    • 앞선 필터 처리 과정에서 인증 예외(AuthenticationException) 또는 인가 예외(AccessDeniedException)가 발생한 경우, 해당 예외를 캐치하여 처리하는 필터 (모든 예외를 다 이 필터에서 처리하는 건 아님)
  9. *FilterSecurityInterceptor, to protect web URIs and raise exceptions when access is denied
    • 인가(Authorization)를 결정하는 AccessDecisionManager에게 접근 권한이 있는지 확인하고 denied인 경우 예외를 던짐.

기타 filters

  • WebAsyncManagerIntegrationFilter 비동기 요청에 대해 Authentication객체를 사용할 수 있도록 처리해주는 필터
  • SessionManagementFilter 세션 변조 공격 방지 (SessionId를 계속 다르게 변경해서 클라이언트에 내려준다) 유효하지 않은 세션으로 접근했을 때 URL 핸들링.
    • 하나의 세션 아이디로 접속하는 최대 세션 수(동시 접속) 설정
    • 세션 생성 전략 설정
  • HeaderWriterFilter 응답 Header를 작성하는 필터
  • CsrfFilter csrf 공격을 방어하는 필터
  • LogoutFilter 로그아웃 요청을 처리하는 필터
  • RequestCacheAwareFilter 인증 후, 원래 Request 정보로 재구성하는 필터

How Spring Security Filter Chain works

security filter chain은 강력하고, 유연해서 처리할 필터의 종류와 순서를 커스터마이징 할 수 있다.

 

 

'Dev > SPRING' 카테고리의 다른 글

[SPRING] Springboot  (0) 2023.03.06
[SPRING] AOP(Aspect Oriented Programming) 관점 지향 프로그래밍  (0) 2023.02.27
[SPRING] 스프링 개요  (1) 2023.02.26
[SPRING] Transaction 선언  (0) 2023.02.25