Skip to content

Commit 6e6d382

Browse files
committed
Adapt to WebClient's new exception wrapping
See spring-projects/spring-framework#23842 Closes gh-9031
1 parent 65f7885 commit 6e6d382

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/userinfo/DefaultReactiveOAuth2UserService.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

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

19-
import java.io.IOException;
2019
import java.util.HashSet;
2120
import java.util.Map;
2221
import java.util.Set;
@@ -30,7 +29,6 @@
3029
import org.springframework.http.HttpHeaders;
3130
import org.springframework.http.HttpStatus;
3231
import org.springframework.http.MediaType;
33-
import org.springframework.security.authentication.AuthenticationServiceException;
3432
import org.springframework.security.core.GrantedAuthority;
3533
import org.springframework.security.core.authority.SimpleGrantedAuthority;
3634
import org.springframework.security.oauth2.core.AuthenticationMethod;
@@ -131,15 +129,15 @@ public Mono<OAuth2User> loadUser(OAuth2UserRequest userRequest) throws OAuth2Aut
131129

132130
return new DefaultOAuth2User(authorities, attrs, userNameAttributeName);
133131
})
134-
.onErrorMap(IOException.class,
135-
(ex) -> new AuthenticationServiceException("Unable to access the userInfoEndpoint " + userInfoUri,
136-
ex)
137-
)
138-
.onErrorMap(UnsupportedMediaTypeException.class, (ex) -> {
132+
.onErrorMap((ex) -> (ex instanceof UnsupportedMediaTypeException ||
133+
ex.getCause() instanceof UnsupportedMediaTypeException), (ex) -> {
134+
String contentType = (ex instanceof UnsupportedMediaTypeException) ?
135+
((UnsupportedMediaTypeException) ex).getContentType().toString() :
136+
((UnsupportedMediaTypeException) ex.getCause()).getContentType().toString();
139137
String errorMessage = "An error occurred while attempting to retrieve the UserInfo Resource from '"
140138
+ userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint()
141139
.getUri()
142-
+ "': response contains invalid content type '" + ex.getContentType().toString() + "'. "
140+
+ "': response contains invalid content type '" + contentType + "'. "
143141
+ "The UserInfo Response should return a JSON object (content type 'application/json') "
144142
+ "that contains a collection of name and value pairs of the claims about the authenticated End-User. "
145143
+ "Please ensure the UserInfo Uri in UserInfoEndpoint for Client Registration '"
@@ -150,10 +148,10 @@ public Mono<OAuth2User> loadUser(OAuth2UserRequest userRequest) throws OAuth2Aut
150148
null);
151149
throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString(), ex);
152150
})
153-
.onErrorMap((t) -> !(t instanceof AuthenticationServiceException), (t) -> {
151+
.onErrorMap((ex) -> {
154152
OAuth2Error oauth2Error = new OAuth2Error(INVALID_USER_INFO_RESPONSE_ERROR_CODE,
155-
"An error occurred reading the UserInfo Success response: " + t.getMessage(), null);
156-
return new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString(), t);
153+
"An error occurred reading the UserInfo response: " + ex.getMessage(), null);
154+
return new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString(), ex);
157155
});
158156
});
159157
// @formatter:on

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/userinfo/DefaultReactiveOAuth2UserServiceTests.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.springframework.http.HttpHeaders;
3838
import org.springframework.http.HttpMethod;
3939
import org.springframework.http.MediaType;
40-
import org.springframework.security.authentication.AuthenticationServiceException;
4140
import org.springframework.security.core.GrantedAuthority;
4241
import org.springframework.security.core.authority.SimpleGrantedAuthority;
4342
import org.springframework.security.oauth2.client.registration.ClientRegistration;
@@ -219,9 +218,9 @@ public void loadUserWhenUserInfoErrorResponseThenThrowOAuth2AuthenticationExcept
219218
}
220219

221220
@Test
222-
public void loadUserWhenUserInfoUriInvalidThenThrowAuthenticationServiceException() {
221+
public void loadUserWhenUserInfoUriInvalidThenThrowOAuth2AuthenticationException() {
223222
this.clientRegistration.userInfoUri("https://invalid-provider.com/user");
224-
assertThatExceptionOfType(AuthenticationServiceException.class)
223+
assertThatExceptionOfType(OAuth2AuthenticationException.class)
225224
.isThrownBy(() -> this.userService.loadUser(oauth2UserRequest()).block());
226225
}
227226

oauth2/oauth2-jose/src/test/java/org/springframework/security/oauth2/jwt/NimbusReactiveJwtDecoderTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void decodeWhenInvalidUrl() {
138138
// @formatter:off
139139
assertThatIllegalStateException()
140140
.isThrownBy(() -> this.decoder.decode(this.messageReadToken).block())
141-
.withCauseInstanceOf(UnknownHostException.class);
141+
.withRootCauseInstanceOf(UnknownHostException.class);
142142
// @formatter:on
143143
}
144144

0 commit comments

Comments
 (0)