|
23 | 23 | import org.springframework.security.core.GrantedAuthority;
|
24 | 24 | import org.springframework.security.core.context.ReactiveSecurityContextHolder;
|
25 | 25 | import org.springframework.security.core.context.SecurityContextHolder;
|
| 26 | +import org.springframework.security.oauth2.client.ClientAuthorizationRequiredException; |
26 | 27 | import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
|
27 | 28 | import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
|
28 | 29 | import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
@@ -150,7 +151,13 @@ public Consumer<WebClient.RequestHeadersSpec<?>> defaultRequest() {
|
150 | 151 | * @return the {@link Consumer} to populate the attributes
|
151 | 152 | */
|
152 | 153 | public static Consumer<Map<String, Object>> oauth2AuthorizedClient(OAuth2AuthorizedClient authorizedClient) {
|
153 |
| - return attributes -> attributes.put(OAUTH2_AUTHORIZED_CLIENT_ATTR_NAME, authorizedClient); |
| 154 | + return attributes -> { |
| 155 | + if (authorizedClient == null) { |
| 156 | + attributes.remove(OAUTH2_AUTHORIZED_CLIENT_ATTR_NAME); |
| 157 | + } else { |
| 158 | + attributes.put(OAUTH2_AUTHORIZED_CLIENT_ATTR_NAME, authorizedClient); |
| 159 | + } |
| 160 | + }; |
154 | 161 | }
|
155 | 162 |
|
156 | 163 | /**
|
@@ -262,6 +269,9 @@ private void populateDefaultOAuth2AuthorizedClient(Map<String, Object> attrs) {
|
262 | 269 | OAuth2AuthorizedClient authorizedClient = this.authorizedClientRepository
|
263 | 270 | .loadAuthorizedClient(clientRegistrationId, authentication,
|
264 | 271 | request);
|
| 272 | + if (authorizedClient == null) { |
| 273 | + throw new ClientAuthorizationRequiredException(clientRegistrationId); |
| 274 | + } |
265 | 275 | oauth2AuthorizedClient(authorizedClient).accept(attrs);
|
266 | 276 | }
|
267 | 277 | }
|
|
0 commit comments