리프레시 토큰 재발급 API를 만들면서 문제가 발생했다.
API의 요구사항은 다음과 같다.
- accessToken, refreshToken 2개의 쿠키를 필수로 전달.
- jwt 필터 건너뜀.
- 스프링 시큐리티 건너뜀.
즉 2개의 쿠키를 전달해 준다면 일치하는 리프레시를 찾아서 살아있다면 재발급을 진행해 주는 형식이다.
컨트롤러
@PostMapping("refresh-token")
public ResponseEntity<ApiResponse<?>> refreshToken(@CookieValue(value = "accessToken") @NotNull(message = "토큰이 없습니다.") String accessToken,
@CookieValue(value = "refreshToken") @NotNull(message = "토큰이 없습니다.") String refreshToken,
HttpServletResponse response) {
refreshTokenService.reGenerateToken(response, accessToken, refreshToken);
return ResponseEntity.ok(ApiResponse.createSuccessNoContent("토큰 재발급 완료"));
}
전체 경로는 api/auth/refresh-token으로 구성했으며 쿠키 2개를 필수로 받고 있다.
시큐리티
해당 api 경로를 permit 해주었고, jwt 필터 또한 안 타도록 설정했다.
이후 postman을 돌려보면
뜬금없이 oauth2 로그인을 타는 것 같다. Status도 200으로 오고 뭔가 이상함을 느꼈다.
분명 저번에도 이 에러를 많이 마주쳤었는데 매번 oauth가 문제다 문제
로그도 아무 것도 찍히지 않았다. 변경 사항이라고는 아래처럼 kakao 쿠키가 추가된 것.
그래서 시큐리티 설정에 debug를 활성화시켜 로그를 파악해보려고 한다.
@EnableWebSecurity(debug = true)
활성화를 시키고 진행을 해보면
일단 요청은 제대로 들어온 것을 확인했다.
그리고 이제 알았는데 필터 체인도 보여준다. 이게 순서를 의미하나? 자세한 내용은 조금 찾아봐야 할 것 같다.
이후 발생했던 로그다. accessToken 쿠키가 존재하지 않다. 그니깐 요청은 제대로 받고 permit도 됐었는데 왜 oauth를 탔던 걸까?
에러를 발생시킨 후 /error로 요청을 리다이렉트 시켰다.
그다음에 갑자기 oauth2 로직이 실행된다. 왜??
뭔가 어디선가 error로 리다이렉트 하면 oauth2 로직을 실행시키게 하는 것 같다. 근데 해당 로직을 제대로 파악하지 못해서 우선 긴급 처방을 해보자.
시큐리티 Config에 다음과 같이 error를 핸들링해주면 된다.
.authorizeHttpRequests(authorize ->
authorize
.dispatcherTypeMatchers(DispatcherType.ERROR).permitAll()
에러 처리 과정중에 생성된 요청을 허용시키는 것.
아마 /error로 리다이렉트를 시키면서 다시 시큐리티를 타게 됐고
/error에 대한 요청 패스는 없다 보니 권한이 필요해서 oauth2 로직을 그대로 타게 된 것 같다.
그래서 일단 400 에러를 내뱉게는 만들었다.
근데 이제 고민인 점이 이렇게 재발급 API에 400 에러를 뱉으면 로그아웃을 시키는 게 맞을지
아니면 쿠키를 required = false로 둬서 null을 체크한 다음 글로벌 익셉션 핸들러로 throw를 하는 게 나을지 모르겠다.
'오류해결' 카테고리의 다른 글
스프링 시큐리티에서 인증 후 CSRF 토큰 갱신 문제 (0) | 2024.08.07 |
---|---|
pinia의 piniaPluginPersistedstate가 적용이 안 된다. (0) | 2024.05.15 |
QClass import가 불가능한 문제 (0) | 2024.04.16 |
Did not find any jobs with GenericTrigger configured! (젠킨스 에러) (0) | 2024.04.04 |