본문 바로가기

Programming/Web-Spring

[SpringBoot] 포스트맨 실행 시 에러 Error: Exceeded maxRedirects. Probably stuck in a redirect loop

현재 상황

- 인증, 인가 관련 예외 처리를 진행하던 중, AuthenticationEntryPoint에서 응답을 주기 위해서 redirect를 하고 있었다.

- 흐름은 AuthenticationEntryPoint -> sendRedirect("/exception/**")

-> ExceptionController에서 throw new CustomException -> ControllerAdvice에서 해당 exception에 대한 예외 처리

- 그러나 해당 response가 Controller까지 도달하지 못하고 계속 요청이 동작하지 않는다.

- 프로젝트에는 스프링 시큐리티가 적용되어 있다.

 

문제

Could not get response

Error: Exceeded maxRedirects. Probably stuck in a redirect loop 

 

- 목적지에 도착하지 못하고 계속 loop이 돌다가 너무 많은 redirection이 생성.

- 따라서 sendRedirect가 왜 동작하지 않는지 원인 분석을 했다.

 

디버깅

- 해당 AuthenticationEntryPoint에 breakPoint를 찍어 두고, 디버깅을 했는데 해당 url의 요청을 받는 Controller까지 요청이 가지 못하는 것을 확인했다.

- 필터에서는 sendRedirect가 동작하지 않는지 등에 대해 검색을 했다.

 

https://stackoverflow.com/questions/10917242/redirection-from-servlet-filter-does-not-work

 

Redirection from Servlet/Filter does not work

I have a problem with redirection - it simply does not work for valid paths. Right now I use page forwarding in the Servlet, but I need redirection in a filter. All the pages reside in the 'pages'

stackoverflow.com

해당 stackoverflow의 답변이 정답은 아니었지만, /*을 보고 한 가지가 스쳤다.

controller가 요청을 받지 못하고, 해당 url까지 전달되지 않는다면 설마 스프링 시큐리티?

 

따라서 설마 하는 마음으로 바로 스프링 시큐리티에 해당 url을 permitAll을 해주었다.

 

.antMatchers(HttpMethod.GET, "/exception/**").permitAll()

 

이후에 다시 요청을 보냈더니, 정상적으로 url에 요청이 전달되었다.

 

결론

- sendRedirect(url)의 get 요청이 스프링 시큐리티 필터에 걸려서 동작하지 않았던 것이다.

- 시큐리티를 사용하는데 동작하지 않는 경우 한번 살펴보자

728x90