이전글:

2023.12.10 - [웹/Spring] - Spring Security 구조에 대해서 - Adding a CustomFilter to the FilterChain

 

Spring Secuiryt 구조에 대해서 - Adding a CustomFilter to the FilterChain

이전글: 2023.12.10 - [웹/Spring] - Spring Security 구조에 대해서 - Printing the Security Filters Spring Security 구조에 대해서 - Printing the Security Filters 이전글: 2023.12.09 - [웹/Spring] - Spring Security 구조에 대해서 -Secur

kwaksh2319.tistory.com

 

security filter도 예외처리를 다뤄주는게 필요하겠죠. 그래서 spring  security 구조에 대해서도 다루고 있습니다.

 

ExceptionTranslationFilter는 'AccessDeniedException' 및 'AuthenticaitonException' 을 적절한 HTTP 응답으로 변환하는 Spring Security의 구성요소 입니다. 

 

ExceptionTranslationFilter는 Security Filter 중 하나로써 FilterChainProxy에 삽입됩니다.

아래 이미지를 확인하겠습니다.

 

1) 첫번쨰로는  ExceptionTranslationFilter는 애플리케이션의 FilterChain.doFilter(Request,Response) 호출하여 나머지 부분을 실행합니다.

2) 만약 유저가 인증되지 않았거나 AuthenicationException인 경우 인증을 시작합니다.

  •    SecurityContextHolder가 초기화 됩니다.
  •   HttpServletRequest는 저장되어 인증이 성공한후 원래 쵸엉을 다시 실행한느데 사용될수 있습니다.
  •  AuthenticationEntryPoint는 클라이언트로부터 자격 증명을 요청하는데 사용될수 있습니다. 예를들어 로그인 페이지로 리다이렉트 하거나 WWW-Authenticate 헤더를 보낼수 있습니다.

3)  그렇지 않고 3번으로 진행되면 AccessDeniedException인경우 접근이 거부됩니다. 접근거부처리 되면서 AccessDeniedHander가 호출됩니다.

 

 

ExceptionTranlationFilter에 대허서 코드로 확인해보겠습니다.

try {
	filterChain.doFilter(request, response);//(1)
} catch (AccessDeniedException | AuthenticationException ex) {
	if (!authenticated || ex instanceof AuthenticationException) {
		startAuthentication();//(2)
	} else {
		accessDenied();//(3)
	}
}

(1) A review of  Filter에서 설명한 바와 같이 'FilterChain.doFilter(request,response)'를 호출하는 것은 애플리케이션의 나머지 부분을 호출하는것과 같습니다. 이는 다른부분의 애플리케이션(예:FilterSecurityInterceptor 또는 메소드 보안)이 'AuthenticationException' 또는 'AccessDeniedException' 을 발생시킬 경우, 여기서 처리하는을 의미합니다.

(2) 사용자가 인증되지 않았거나 'AuthenticationException'인경우, 인증을 시작합니다.

(3)그렇지 않으면  접근을 거부합니다.

https://docs.spring.io/spring-security/reference/servlet/architecture.html#servlet-authorization-filtersecurityinterceptor

 

Architecture :: Spring Security

The Security Filters are inserted into the FilterChainProxy with the SecurityFilterChain API. Those filters can be used for a number of different purposes, like authentication, authorization, exploit protection, and more. The filters are executed in a spec

docs.spring.io

 

 

+ Recent posts