Browse Source

Adapt to WebClient's new exception wrapping

See https://github.com/spring-projects/spring-framework/issues/23842

Closes gh-9031
Joe Grandja 5 years ago
parent
commit
6e6d382357

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

@@ -16,7 +16,6 @@
 
 
 package org.springframework.security.oauth2.client.userinfo;
 package org.springframework.security.oauth2.client.userinfo;
 
 
-import java.io.IOException;
 import java.util.HashSet;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Map;
 import java.util.Set;
 import java.util.Set;
@@ -30,7 +29,6 @@ import org.springframework.core.ParameterizedTypeReference;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.http.MediaType;
-import org.springframework.security.authentication.AuthenticationServiceException;
 import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.security.oauth2.core.AuthenticationMethod;
 import org.springframework.security.oauth2.core.AuthenticationMethod;
@@ -131,15 +129,15 @@ public class DefaultReactiveOAuth2UserService implements ReactiveOAuth2UserServi
 
 
 				return new DefaultOAuth2User(authorities, attrs, userNameAttributeName);
 				return new DefaultOAuth2User(authorities, attrs, userNameAttributeName);
 			})
 			})
-			.onErrorMap(IOException.class,
-					(ex) -> new AuthenticationServiceException("Unable to access the userInfoEndpoint " + userInfoUri,
-							ex)
-			)
-			.onErrorMap(UnsupportedMediaTypeException.class, (ex) -> {
+			.onErrorMap((ex) -> (ex instanceof UnsupportedMediaTypeException ||
+					ex.getCause() instanceof UnsupportedMediaTypeException), (ex) -> {
+				String contentType = (ex instanceof UnsupportedMediaTypeException) ?
+						((UnsupportedMediaTypeException) ex).getContentType().toString() :
+						((UnsupportedMediaTypeException) ex.getCause()).getContentType().toString();
 				String errorMessage = "An error occurred while attempting to retrieve the UserInfo Resource from '"
 				String errorMessage = "An error occurred while attempting to retrieve the UserInfo Resource from '"
 						+ userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint()
 						+ userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint()
 								.getUri()
 								.getUri()
-						+ "': response contains invalid content type '" + ex.getContentType().toString() + "'. "
+						+ "': response contains invalid content type '" + contentType + "'. "
 						+ "The UserInfo Response should return a JSON object (content type 'application/json') "
 						+ "The UserInfo Response should return a JSON object (content type 'application/json') "
 						+ "that contains a collection of name and value pairs of the claims about the authenticated End-User. "
 						+ "that contains a collection of name and value pairs of the claims about the authenticated End-User. "
 						+ "Please ensure the UserInfo Uri in UserInfoEndpoint for Client Registration '"
 						+ "Please ensure the UserInfo Uri in UserInfoEndpoint for Client Registration '"
@@ -150,10 +148,10 @@ public class DefaultReactiveOAuth2UserService implements ReactiveOAuth2UserServi
 						null);
 						null);
 				throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString(), ex);
 				throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString(), ex);
 			})
 			})
-			.onErrorMap((t) -> !(t instanceof AuthenticationServiceException), (t) -> {
+			.onErrorMap((ex) -> {
 				OAuth2Error oauth2Error = new OAuth2Error(INVALID_USER_INFO_RESPONSE_ERROR_CODE,
 				OAuth2Error oauth2Error = new OAuth2Error(INVALID_USER_INFO_RESPONSE_ERROR_CODE,
-						"An error occurred reading the UserInfo Success response: " + t.getMessage(), null);
-				return new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString(), t);
+						"An error occurred reading the UserInfo response: " + ex.getMessage(), null);
+				return new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString(), ex);
 			});
 			});
 		});
 		});
 		// @formatter:on
 		// @formatter:on

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

@@ -37,7 +37,6 @@ import org.springframework.core.ParameterizedTypeReference;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpMethod;
 import org.springframework.http.HttpMethod;
 import org.springframework.http.MediaType;
 import org.springframework.http.MediaType;
-import org.springframework.security.authentication.AuthenticationServiceException;
 import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.GrantedAuthority;
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.security.core.authority.SimpleGrantedAuthority;
 import org.springframework.security.oauth2.client.registration.ClientRegistration;
 import org.springframework.security.oauth2.client.registration.ClientRegistration;
@@ -219,9 +218,9 @@ public class DefaultReactiveOAuth2UserServiceTests {
 	}
 	}
 
 
 	@Test
 	@Test
-	public void loadUserWhenUserInfoUriInvalidThenThrowAuthenticationServiceException() {
+	public void loadUserWhenUserInfoUriInvalidThenThrowOAuth2AuthenticationException() {
 		this.clientRegistration.userInfoUri("https://invalid-provider.com/user");
 		this.clientRegistration.userInfoUri("https://invalid-provider.com/user");
-		assertThatExceptionOfType(AuthenticationServiceException.class)
+		assertThatExceptionOfType(OAuth2AuthenticationException.class)
 				.isThrownBy(() -> this.userService.loadUser(oauth2UserRequest()).block());
 				.isThrownBy(() -> this.userService.loadUser(oauth2UserRequest()).block());
 	}
 	}
 
 

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

@@ -138,7 +138,7 @@ public class NimbusReactiveJwtDecoderTests {
 		// @formatter:off
 		// @formatter:off
 		assertThatIllegalStateException()
 		assertThatIllegalStateException()
 				.isThrownBy(() -> this.decoder.decode(this.messageReadToken).block())
 				.isThrownBy(() -> this.decoder.decode(this.messageReadToken).block())
-				.withCauseInstanceOf(UnknownHostException.class);
+				.withRootCauseInstanceOf(UnknownHostException.class);
 		// @formatter:on
 		// @formatter:on
 	}
 	}