본문 바로가기
Back-end/Spring

HttpClientException 401 Unauthorized

by JiGyeong 2021. 7. 12.

 

org.springframework.web.client.HttpClientErrorException: 401 Unauthorized at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:88) at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:533) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:489) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:447) at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:318)

 

분명 직접 API 호출했을때는 문제없이 200 이 떨어졌는데

Application 서버에서 호출할때는 401 Unauthorized 에러가 떨어졌습니다.

파라미터를 모두 똑같이 적용해봐도 직접호출할땐 200 서버에서 나갈땐 401 이 떨어져 구글링을 해보았습니다.

구글링을 해보았더니 다행히 저와 같은증상인 사람이 많았습니다.

이유인 즉슨... RestTemplate 자체에서 예외를 발생시키기 때문이었습니다.

 

연속으로 호출할 경우 나머지 호출이 반환되면 UNAUTHORIZEDRestTemplate에 의해 응답 본문이 손실/무시됩니다.
그러나 GUI 클라이언트를 사용하여 동일한 요청을 수행하면 UNAUTHORIZED상태 및 비어 있지 않은 응답 본문이 반환됩니다.

 

기본 설정으로 RestTemplate에서 401 응답을 처리하는 것은 매우 어렵습니다(불가능). 실제로는 가능하지만 오류 처리기와 요청 팩토리를 제공해야 합니다. 오류 핸들러는 분명했지만 문제는 기본 요청 팩토리가 응답의 상태 코드를 보려고 할 때 (분명히 사용 가능함에도 불구하고) HttpRetryException을 던질 수 있는 java.net을 사용한다는 것입니다. 해결책은 HttpComponentsClientHttpRequestFactory를 사용하는 것입니다. 

 

그래서 아래와같이 기본 restTemplate 만 선언하여 사용하던 곳에

RequestFactory를 사용하고 ErrorHandler를 통해 처리할수 있었습니다.

 

 

 

 

참고

 

 

https://stackoverflow.com/questions/15404605/spring-resttemplate-invoking-webservice-with-errors-and-analyze-status-code

 

Spring RestTemplate invoking webservice with errors and analyze status code

I designed a webservice to perform a task if request parameters are OK, or return 401 Unauthorized HTTP status code if request parameters are wrong or empty. I'm using RestTemplate to perform a te...

stackoverflow.com

 

https://stackoverflow.com/questions/16748969/java-net-httpretryexception-cannot-retry-due-to-server-authentication-in-strea

 

java.net.HttpRetryException: cannot retry due to server authentication, in streaming mode

We have two parts in our app: Server - provide REST services Client - consume them via Spring restTemplate In addition to the HTTP status our server returns an HTTP body with JSON that describe er...

stackoverflow.com

https://github.com/spring-projects/spring-framework/issues/14004

 

Update reference documentation on handling 401 response in the RestTemplate [SPR-9367] · Issue #14004 · spring-projects/spring

Dave Syer opened SPR-9367 and commented It is very difficult (impossible) to handle a 401 response in the RestTemplate with default settings (Documentation?). In fact it is possible, but you have t...

github.com

 

https://github.com/spring-projects/spring-framework/issues/21321

 

RestTemplate - providing custom ResponseErrorHandler does not prevent an error with http 401 responses [SPR-16781] · Issue #213

Jeff Synnestvedt opened SPR-16781 and commented When a rest template is customized with a ResponseErrorHandler that does not return true on hasError or does not throw an exception in handleError fo...

github.com

 

'Back-end > Spring' 카테고리의 다른 글

Spring Boot JUnit Test , HttpRequest  (0) 2019.06.13
스프링 공부  (0) 2019.06.11
Spring Framework 실행순서  (1) 2019.03.22
Received fatal alert: protocol_version -> [Help 1] 에러해결  (0) 2019.03.21
Mybatis 연동하기  (0) 2019.03.04