mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-01 09:42:13 +00:00
ResponseErrorHandler.handleError(URI, HttpMethod,ClientHttpResponse)
Closes gh-17056
This commit is contained in:
parent
3976e7d456
commit
5704582c52
@ -17,10 +17,12 @@
|
|||||||
package org.springframework.security.oauth2.client.http;
|
package org.springframework.security.oauth2.client.http;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
import com.nimbusds.oauth2.sdk.token.BearerTokenError;
|
import com.nimbusds.oauth2.sdk.token.BearerTokenError;
|
||||||
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.client.ClientHttpResponse;
|
import org.springframework.http.client.ClientHttpResponse;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
@ -53,9 +55,9 @@ public class OAuth2ErrorResponseErrorHandler implements ResponseErrorHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleError(ClientHttpResponse response) throws IOException {
|
public void handleError(URI url, HttpMethod method, ClientHttpResponse response) throws IOException {
|
||||||
if (HttpStatus.BAD_REQUEST.value() != response.getStatusCode().value()) {
|
if (HttpStatus.BAD_REQUEST.value() != response.getStatusCode().value()) {
|
||||||
this.defaultErrorHandler.handleError(response);
|
this.defaultErrorHandler.handleError(url, method, response);
|
||||||
}
|
}
|
||||||
// A Bearer Token Error may be in the WWW-Authenticate response header
|
// A Bearer Token Error may be in the WWW-Authenticate response header
|
||||||
// See https://tools.ietf.org/html/rfc6750#section-3
|
// See https://tools.ietf.org/html/rfc6750#section-3
|
||||||
|
@ -17,10 +17,12 @@
|
|||||||
package org.springframework.security.oauth2.client.http;
|
package org.springframework.security.oauth2.client.http;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.client.ClientHttpResponse;
|
import org.springframework.http.client.ClientHttpResponse;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
@ -45,6 +47,10 @@ public class OAuth2ErrorResponseErrorHandlerTests {
|
|||||||
|
|
||||||
private OAuth2ErrorResponseErrorHandler errorHandler = new OAuth2ErrorResponseErrorHandler();
|
private OAuth2ErrorResponseErrorHandler errorHandler = new OAuth2ErrorResponseErrorHandler();
|
||||||
|
|
||||||
|
private URI anyURi = URI.create("/any");
|
||||||
|
|
||||||
|
private HttpMethod anyMethod = HttpMethod.GET;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void handleErrorWhenErrorResponseBodyThenHandled() {
|
public void handleErrorWhenErrorResponseBodyThenHandled() {
|
||||||
// @formatter:off
|
// @formatter:off
|
||||||
@ -55,7 +61,7 @@ public class OAuth2ErrorResponseErrorHandlerTests {
|
|||||||
// @formatter:on
|
// @formatter:on
|
||||||
MockClientHttpResponse response = new MockClientHttpResponse(errorResponse.getBytes(), HttpStatus.BAD_REQUEST);
|
MockClientHttpResponse response = new MockClientHttpResponse(errorResponse.getBytes(), HttpStatus.BAD_REQUEST);
|
||||||
assertThatExceptionOfType(OAuth2AuthorizationException.class)
|
assertThatExceptionOfType(OAuth2AuthorizationException.class)
|
||||||
.isThrownBy(() -> this.errorHandler.handleError(response))
|
.isThrownBy(() -> this.errorHandler.handleError(this.anyURi, this.anyMethod, response))
|
||||||
.withMessage("[unauthorized_client] The client is not authorized");
|
.withMessage("[unauthorized_client] The client is not authorized");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +80,7 @@ public class OAuth2ErrorResponseErrorHandlerTests {
|
|||||||
.willReturn(new OAuth2Error("unauthorized_client", "The client is not authorized", null));
|
.willReturn(new OAuth2Error("unauthorized_client", "The client is not authorized", null));
|
||||||
|
|
||||||
assertThatExceptionOfType(OAuth2AuthorizationException.class)
|
assertThatExceptionOfType(OAuth2AuthorizationException.class)
|
||||||
.isThrownBy(() -> this.errorHandler.handleError(response))
|
.isThrownBy(() -> this.errorHandler.handleError(this.anyURi, this.anyMethod, response))
|
||||||
.withMessage("[unauthorized_client] The client is not authorized");
|
.withMessage("[unauthorized_client] The client is not authorized");
|
||||||
verify(oauth2ErrorConverter).read(eq(OAuth2Error.class), eq(response));
|
verify(oauth2ErrorConverter).read(eq(OAuth2Error.class), eq(response));
|
||||||
}
|
}
|
||||||
@ -85,7 +91,7 @@ public class OAuth2ErrorResponseErrorHandlerTests {
|
|||||||
MockClientHttpResponse response = new MockClientHttpResponse(new byte[0], HttpStatus.BAD_REQUEST);
|
MockClientHttpResponse response = new MockClientHttpResponse(new byte[0], HttpStatus.BAD_REQUEST);
|
||||||
response.getHeaders().add(HttpHeaders.WWW_AUTHENTICATE, wwwAuthenticateHeader);
|
response.getHeaders().add(HttpHeaders.WWW_AUTHENTICATE, wwwAuthenticateHeader);
|
||||||
assertThatExceptionOfType(OAuth2AuthorizationException.class)
|
assertThatExceptionOfType(OAuth2AuthorizationException.class)
|
||||||
.isThrownBy(() -> this.errorHandler.handleError(response))
|
.isThrownBy(() -> this.errorHandler.handleError(this.anyURi, this.anyMethod, response))
|
||||||
.withMessage("[insufficient_scope] The access token expired");
|
.withMessage("[insufficient_scope] The access token expired");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +101,7 @@ public class OAuth2ErrorResponseErrorHandlerTests {
|
|||||||
MockClientHttpResponse response = new MockClientHttpResponse(new byte[0], HttpStatus.BAD_REQUEST);
|
MockClientHttpResponse response = new MockClientHttpResponse(new byte[0], HttpStatus.BAD_REQUEST);
|
||||||
response.getHeaders().add(HttpHeaders.WWW_AUTHENTICATE, invalidWwwAuthenticateHeader);
|
response.getHeaders().add(HttpHeaders.WWW_AUTHENTICATE, invalidWwwAuthenticateHeader);
|
||||||
assertThatExceptionOfType(OAuth2AuthorizationException.class)
|
assertThatExceptionOfType(OAuth2AuthorizationException.class)
|
||||||
.isThrownBy(() -> this.errorHandler.handleError(response))
|
.isThrownBy(() -> this.errorHandler.handleError(this.anyURi, this.anyMethod, response))
|
||||||
.withMessage("[server_error] ");
|
.withMessage("[server_error] ");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,7 +109,7 @@ public class OAuth2ErrorResponseErrorHandlerTests {
|
|||||||
public void handleErrorWhenErrorResponseWithInvalidStatusCodeThenHandled() {
|
public void handleErrorWhenErrorResponseWithInvalidStatusCodeThenHandled() {
|
||||||
CustomMockClientHttpResponse response = new CustomMockClientHttpResponse(new byte[0], 596);
|
CustomMockClientHttpResponse response = new CustomMockClientHttpResponse(new byte[0], 596);
|
||||||
assertThatExceptionOfType(IllegalArgumentException.class)
|
assertThatExceptionOfType(IllegalArgumentException.class)
|
||||||
.isThrownBy(() -> this.errorHandler.handleError(response))
|
.isThrownBy(() -> this.errorHandler.handleError(this.anyURi, this.anyMethod, response))
|
||||||
.withMessage("No matching constant for [596]");
|
.withMessage("No matching constant for [596]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user