728x90
반응형

[Spring Security] Spring Security 내부 구조 총정리 | 필터 체인부터 인증 과정까지 완벽 분석 - 2

 

[Spring Security] Spring Security 내부 구조 총정리 | 필터 체인부터 인증 과정까지 완벽 분석 - 2

[Spring Security] Spring Security 내부 구조 총정리 | 필터 체인부터 인증 과정까지 완벽 분석 [Spring Security] Spring Security 내부 구조 총정리 | 필터 체인부터 인증 과정까지 완벽 분석[Spring Security] Spring Boot

crushed-taro.tistory.com

1. Security Filter

1. Security Filter 개요

1. Security Filter 주요 역할

  • 일련의 서블릿 필터 체인을 자동으로 구성한다.
  • 일반적으로 웹 환경에서 브라우저가 서버에 요청을 보내게 되면 DispatcherServlet이 요청 받기 이전에 많은 servlet filter를 거치게 된다.

Spring Security

 

  • SecurityContextPersistenceFilter
    1. 해당 필터는 Security에서 제공하는 중요한 필터로 요청 사이에 보안 컨텍스트를 유지하는 임무를 수행하게 된다. 인증된 사용자의 보안 컨텍스트가 여러 요청에서 유지되어 사용자의 인증 상태 및 부여된 권한을 유지하도록 한다.
    2. 애플리케이션에 요청이 들어오면 해당 요청을 가로채서 요청과 관련된 보안 컨텍스트가 있는지 확인하게 되며 존재하는 경우 요청 동안(session) SecurityContextHolder에 설정된다.
    3. 만약 비어있는 경우 새로운 보안 context를 생성한다.
http
            // ...
            .addFilterBefore(new SecurityContextPersistenceFilter(securityContextRepository), BasicAuthenticationFilter.class);
            // ...

 

  • LogutFilter
    1. 인증된 사용자의 로그아웃 프로세스를 처리하는 Spring Security 필터이다. 로그아웃 요청을 가로채고 세션 무효화, 인증 지우기, 지정된 로그아웃 성공 url로 리다이렉션과 같이 사용자를 로그아웃하는데 필요한 작업을 수행한다.
      1. 사용자 인증 취소 : contextHolder에서 사용자 인증을 삭제하여 사용자를 로그아웃
      2. 사용자 세션 무효화 : http 세션과 연결된 경우 logoutFilter는 해당 세션을 무효로 하여 사용자가 세션 내에서 더 이상 인증되지 않도록 할 수 있다.
      3. 로그아웃 관련 작업 수행 : LogoutFilter는 쿠키 지우기, Remember-me 토큰 제거 또는 사용자 정의 로그아웃 핸들러 호출과 같은 추가 작업을 실행할 수 있다.
http
            // ...
            .logout()
                .logoutUrl("/logout") // Configure the logout URL
                .logoutSuccessHandler(logoutSuccessHandler) // Specify the LogoutSuccessHandler implementation
                .and()
            // ...

// Add the LogoutFilter to the filter chain
        http.addFilterBefore(new LogoutFilter(logoutSuccessHandler, new SecurityContextLogoutHandler()), LogoutFilter.class);

 

  • UsernamePasswordAuthenticationFilter
    1. 사용자 이름과 암호를 포함하는 인증 요청 처리를 하기 위해 Spring security에서 제공하는 핵심 구성 요소이다. 요청을 가로채고 제공된 자격 증명을 사용해서 사용자의 인증을 시도하게 된다. 사용자 인증을 위해 AuthenticationManager에서 인증 절차를 진행하게 된다. 
public class UsernamePasswordAuthenticationCustomFilter extends UsernamePasswordAuthenticationFilter {
// 로직 작성
}

http.addFilter(new UsernamePasswordAuthenticationCustomFilter()) // 필터 커스텀
728x90
반응형

+ Recent posts