Polish gh-13588

This commit is contained in:
Steve Riesenberg 2024-09-14 13:59:27 -05:00
parent 4a9a350ed0
commit 5d8cf6a8bc
No known key found for this signature in database
GPG Key ID: 3D0169B18AB8F0A9
3 changed files with 5 additions and 6 deletions

View File

@ -816,7 +816,7 @@ public class RestClientConfig {
private static ClientRegistrationIdResolver clientRegistrationIdResolver() { private static ClientRegistrationIdResolver clientRegistrationIdResolver() {
return (request) -> { return (request) -> {
Authentication authentication = SecurityContextHolder.getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return (authentication instanceof OAuth2AuthenticationToken principal) return (authentication instanceof OAuth2AuthenticationToken principal)
? principal.getAuthorizedClientRegistrationId() ? principal.getAuthorizedClientRegistrationId()
: null; : null;

View File

@ -30,6 +30,7 @@ import org.springframework.util.Assert;
* using {@link ClientHttpRequest#getAttributes() attributes}. * using {@link ClientHttpRequest#getAttributes() attributes}.
* *
* @author Steve Riesenberg * @author Steve Riesenberg
* @since 6.4
* @see OAuth2ClientHttpRequestInterceptor * @see OAuth2ClientHttpRequestInterceptor
*/ */
public final class RequestAttributeClientRegistrationIdResolver public final class RequestAttributeClientRegistrationIdResolver

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.security.oauth2.client.web.function.client; package org.springframework.security.oauth2.client.web.client;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -55,8 +55,6 @@ import org.springframework.security.oauth2.client.authentication.OAuth2Authentic
import org.springframework.security.oauth2.client.registration.ClientRegistration; import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.TestClientRegistrations; import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository; import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository;
import org.springframework.security.oauth2.client.web.client.OAuth2ClientHttpRequestInterceptor;
import org.springframework.security.oauth2.client.web.client.RequestAttributeClientRegistrationIdResolver;
import org.springframework.security.oauth2.core.OAuth2AccessToken; import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.core.OAuth2AuthorizationException; import org.springframework.security.oauth2.core.OAuth2AuthorizationException;
import org.springframework.security.oauth2.core.OAuth2Error; import org.springframework.security.oauth2.core.OAuth2Error;
@ -606,7 +604,7 @@ public class OAuth2ClientHttpRequestInterceptorTests {
} }
@Test @Test
public void interceptWhenClientRegistrationIdResolverSetThenUsed() { public void interceptWhenCustomClientRegistrationIdResolverSetThenUsed() {
this.requestInterceptor = new OAuth2ClientHttpRequestInterceptor(this.authorizedClientManager, this.requestInterceptor = new OAuth2ClientHttpRequestInterceptor(this.authorizedClientManager,
this.clientRegistrationIdResolver); this.clientRegistrationIdResolver);
this.requestInterceptor.setAuthorizationFailureHandler(this.authorizationFailureHandler); this.requestInterceptor.setAuthorizationFailureHandler(this.authorizationFailureHandler);
@ -651,7 +649,7 @@ public class OAuth2ClientHttpRequestInterceptorTests {
this.server.verify(); this.server.verify();
verify(this.authorizedClientManager).authorize(this.authorizeRequestCaptor.capture()); verify(this.authorizedClientManager).authorize(this.authorizeRequestCaptor.capture());
verify(this.securityContextHolderStrategy).getContext(); verify(this.securityContextHolderStrategy).getContext();
verifyNoMoreInteractions(this.authorizedClientManager); verifyNoMoreInteractions(this.authorizedClientManager, this.securityContextHolderStrategy);
OAuth2AuthorizeRequest authorizeRequest = this.authorizeRequestCaptor.getValue(); OAuth2AuthorizeRequest authorizeRequest = this.authorizeRequestCaptor.getValue();
assertThat(authorizeRequest.getClientRegistrationId()).isEqualTo(this.clientRegistration.getRegistrationId()); assertThat(authorizeRequest.getClientRegistrationId()).isEqualTo(this.clientRegistration.getRegistrationId());
assertThat(authorizeRequest.getPrincipal()).isEqualTo(this.principal); assertThat(authorizeRequest.getPrincipal()).isEqualTo(this.principal);