|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
38 | 38 | import org.springframework.security.oauth2.core.user.OAuth2UserAuthority;
|
39 | 39 | import org.springframework.util.Assert;
|
40 | 40 | import org.springframework.util.StringUtils;
|
| 41 | +import org.springframework.web.reactive.function.UnsupportedMediaTypeException; |
41 | 42 | import org.springframework.web.reactive.function.client.ClientResponse;
|
42 | 43 | import org.springframework.web.reactive.function.client.WebClient;
|
43 | 44 |
|
@@ -140,7 +141,19 @@ public Mono<OAuth2User> loadUser(OAuth2UserRequest userRequest)
|
140 | 141 |
|
141 | 142 | return new DefaultOAuth2User(authorities, attrs, userNameAttributeName);
|
142 | 143 | })
|
143 |
| - .onErrorMap(e -> e instanceof IOException, t -> new AuthenticationServiceException("Unable to access the userInfoEndpoint " + userInfoUri, t)) |
| 144 | + .onErrorMap(IOException.class, e -> new AuthenticationServiceException("Unable to access the userInfoEndpoint " + userInfoUri, e)) |
| 145 | + .onErrorMap(UnsupportedMediaTypeException.class, e -> { |
| 146 | + String errorMessage = "An error occurred while attempting to retrieve the UserInfo Resource from '" + |
| 147 | + userRequest.getClientRegistration().getProviderDetails().getUserInfoEndpoint().getUri() + |
| 148 | + "': response contains invalid content type '" + e.getContentType().toString() + "'. " + |
| 149 | + "The UserInfo Response should return a JSON object (content type 'application/json') " + |
| 150 | + "that contains a collection of name and value pairs of the claims about the authenticated End-User. " + |
| 151 | + "Please ensure the UserInfo Uri in UserInfoEndpoint for Client Registration '" + |
| 152 | + userRequest.getClientRegistration().getRegistrationId() + "' conforms to the UserInfo Endpoint, " + |
| 153 | + "as defined in OpenID Connect 1.0: 'https://openid.net/specs/openid-connect-core-1_0.html#UserInfo'"; |
| 154 | + OAuth2Error oauth2Error = new OAuth2Error(INVALID_USER_INFO_RESPONSE_ERROR_CODE, errorMessage, null); |
| 155 | + throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString(), e); |
| 156 | + }) |
144 | 157 | .onErrorMap(t -> !(t instanceof AuthenticationServiceException), t -> {
|
145 | 158 | OAuth2Error oauth2Error = new OAuth2Error(INVALID_USER_INFO_RESPONSE_ERROR_CODE, "An error occurred reading the UserInfo Success response: " + t.getMessage(), null);
|
146 | 159 | return new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString(), t);
|
|
0 commit comments