Skip to content

Commit de4b3a4

Browse files
author
Steve Riesenberg
committed
Handle custom status codes in error handler
Fixes an issue where custom status codes in the error response cause an IllegalArgumentException to be thrown when resolving an HttpStatus. Closes gh-9741
1 parent e691906 commit de4b3a4

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/http/OAuth2ErrorResponseErrorHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public boolean hasError(ClientHttpResponse response) throws IOException {
5252

5353
@Override
5454
public void handleError(ClientHttpResponse response) throws IOException {
55-
if (!HttpStatus.BAD_REQUEST.equals(response.getStatusCode())) {
55+
if (HttpStatus.BAD_REQUEST.value() != response.getRawStatusCode()) {
5656
this.defaultErrorHandler.handleError(response);
5757
}
5858
// A Bearer Token Error may be in the WWW-Authenticate response header

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/http/OAuth2ErrorResponseErrorHandlerTests.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@
1616

1717
package org.springframework.security.oauth2.client.http;
1818

19+
import java.io.IOException;
20+
1921
import org.junit.Test;
2022

2123
import org.springframework.http.HttpHeaders;
2224
import org.springframework.http.HttpStatus;
25+
import org.springframework.http.client.ClientHttpResponse;
26+
import org.springframework.mock.http.MockHttpInputMessage;
2327
import org.springframework.mock.http.client.MockClientHttpResponse;
2428
import org.springframework.security.oauth2.core.OAuth2AuthorizationException;
29+
import org.springframework.web.client.UnknownHttpStatusCodeException;
2530

2631
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2732

@@ -67,4 +72,48 @@ public void handleErrorWhenErrorResponseWithInvalidWwwAuthenticateHeaderThenHand
6772
.isThrownBy(() -> this.errorHandler.handleError(response)).withMessage("[server_error] ");
6873
}
6974

75+
@Test
76+
public void handleErrorWhenErrorResponseWithInvalidStatusCodeThenHandled() {
77+
CustomMockClientHttpResponse response = new CustomMockClientHttpResponse(new byte[0], 596);
78+
assertThatExceptionOfType(UnknownHttpStatusCodeException.class)
79+
.isThrownBy(() -> this.errorHandler.handleError(response)).withMessage("596 : [no body]");
80+
}
81+
82+
private static final class CustomMockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {
83+
84+
private final int statusCode;
85+
86+
private CustomMockClientHttpResponse(byte[] content, int statusCode) {
87+
super(content);
88+
this.statusCode = statusCode;
89+
}
90+
91+
@Override
92+
public HttpStatus getStatusCode() throws IOException {
93+
return HttpStatus.valueOf(getRawStatusCode());
94+
}
95+
96+
@Override
97+
public int getRawStatusCode() {
98+
return this.statusCode;
99+
}
100+
101+
@Override
102+
public String getStatusText() throws IOException {
103+
HttpStatus httpStatus = HttpStatus.resolve(this.statusCode);
104+
return (httpStatus != null) ? httpStatus.getReasonPhrase() : "";
105+
}
106+
107+
@Override
108+
public void close() {
109+
try {
110+
getBody().close();
111+
}
112+
catch (IOException ex) {
113+
// ignore
114+
}
115+
}
116+
117+
}
118+
70119
}

0 commit comments

Comments
 (0)