SecurityConfig에서 인증 관련 익셉션 필터를 추가할 때 헷갈리는 점이 생겼었다.
.addFilterBefore(new JwtAuthenticationFilter(tokenProvider, customUserDetailsService),
UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(new JwtExceptionFilter(), JwtAuthenticationFilter.class);
지금 보면 exception filter가 authentication filter보다 앞서게 된다.
저걸 보고 아니 인증 과정에서 예외가 발생하면 그 다음 필터에서 해당 예외를 예외처리 해줘야 하는 거 아닌가? 그러니까 authentication filter -> exception filter로 배치하는 게 맞지 않나? 라고 생각했다.
바보였다. 익셉션이 발생하면 앞선 필터로 전파되기 때문에 exception filter가 authentication filter보다 앞에 위치해야 하는 거였다!
익셉션은 상위 레이어로 전파된다는 것을 잊으면 안 된다.
클로드가 시퀀스 다이어그램도 그려줬다. authentication filter에서 exception이 발생하면 exception은 앞 필터인 exception filter에 전달되어 처리된다.