Spring

[Spring Security] 인증, 인가 실패에 대한 Exception 처리 (ExcpetionTranslationFilter, FilterSecurityInterceptor)

2023. 3. 7. 17:52

⚒ 스프링 시큐리티 필터 중 인증과 인가에 대한 필터인 ExceptionTranslationFilter, FilterSecurityInterceptor에 대해 알아보겠다.

🔨 Filter Chain 순서

✔ 위와 같이 ExceptionTranslationFilter와 FilterSecurityInterceptor의 순서는 스프링 시큐리티 필터의 마지막 순서이다.

필터의 흐름을 알아보자.

 

🔨 전제조건

i) 인증이 되지 않음
ii) 인증은 됐지만 권한이 존재하지 않음

◾ 순서1. <ExceptionTranslationFilter.java>

✔ ExceptionTranslationFilter의 doFilter가 실행되면서 다음 필터인 FilterSecurityInterceptor가 실행됨

◾ 순서2. <FilterSecurityInterceptor.java>

i) FilterSecurityInterceptor에서 SecurityContext의 인증객체가 있는지 확인
➡ 없다면 AuthenticationException 발생

ii) 인증객체가 있다면 권한정보를 확인
➡ 요청에 대한 권한이 거부 됐다면 AccessDeniedException 발생

◾ 순서3. <ExceptionTranslationFilter.java>

✔ 순서2 에서 발생한 AuthenticationException과 AccessDeniedException은 그 위인 ExceptionTranslationFilter의 catch문에서 예외를 처리

◾ 순서4. <ExceptionTranslationFilter.java>

✔ AuthenticationException인 경우 AuthenticationEntryPoint가 처리

✔ AccessDeniedException인 경우 AccessDeniedHandler가 처리

 

🔨 Filter 예외 처리

✔ 예외를 공통적으로 처리하기 위해 사용하는 @ControllerAdvice는 Filter, Interceptor단에서 발생하는 Exception은 처리해주지 않는다.
✔ Filter에서의 오류 발생 시 위의 AuthenticationEntryPoint, AccessDeniedHandler 클래스를 구현해서 처리한다.

◾ 순서1. <WebSecurityConfigurerAdapter.java>

✔ WebSecurityConfigurerAdapter를 구현한 Config 파일에서 Custom한 AuthenticationEntryPoint, AccessDeniedHandler 클래스 설정

◾ 순서2. Filter

✔ filter에서 예외 발생 시 exception 변수에 값 설정

✔ request의 Attribute에 exception 변수 set

◾ 순서3. <AuthenticationEntryPoint.java>

✔ request의 Attribute에서 exception 변수 꺼내서 redirect를 한 후 예외 처리가 가능함

'Spring' 카테고리의 다른 글

[Spring] ProxyMode로 Prototype Scope Bean 사용해보기  (0) 2023.03.07
'Spring' 카테고리의 다른 글
  • [Spring] ProxyMode로 Prototype Scope Bean 사용해보기
ttony-kim
ttony-kim
ttony-kim
TisTtony
ttony-kim
전체
오늘
어제
  • Category (16)
    • AWS (5)
    • JAVA (1)
    • Spring (2)
    • Front (1)
    • DB (0)
    • 네트워크 (4)
    • 알고리즘 (1)
    • ELK (0)
    • 기록 (2)
    • Toy Project (0)
    • Infra (0)

인기 글

태그

  • NextJS
  • CertificateManager
  • CertificateMangager
  • 정적웹사이트호스팅
  • 버킷정책생성
  • Subnet
  • 도메인
  • 스프링시큐리티
  • 인프런도장깨기
  • S3
  • 인프런
  • 테스트
  • filterchain
  • InternetGateway
  • VPC
  • route53
  • aws
  • 스프링
  • ImageResize
  • 네트워크
  • 보안그룹
  • 라우팅테이블
  • EC2
  • react
  • ReactQuill
  • Cloudfront
  • BastionHost
  • 객체생성
  • 버킷생성
  • ExcpetionTranslationFilter

최근 댓글

최근 글

hELLO · Designed By 정상우.
ttony-kim
[Spring Security] 인증, 인가 실패에 대한 Exception 처리 (ExcpetionTranslationFilter, FilterSecurityInterceptor)
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.