Browse Source

Inject OAuth2AuthorizedClientManager via constructor.

pull/767/head
Olga Maciaszek-Sharma 2 years ago
parent
commit
77072d96e4
  1. 5
      docs/src/main/asciidoc/_configprops.adoc
  2. 16
      docs/src/main/asciidoc/spring-cloud-openfeign.adoc
  3. 33
      spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignAutoConfiguration.java
  4. 45
      spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/security/OAuth2AccessTokenInterceptor.java
  5. 1
      spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/security/OAuth2FeignRequestInterceptor.java
  6. 2
      spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/security/OAuth2FeignRequestInterceptorBuilder.java
  7. 2
      spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/security/OAuth2FeignRequestInterceptorConfigurer.java
  8. 6
      spring-cloud-openfeign-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json
  9. 31
      spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignAutoConfigurationTests.java
  10. 29
      spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/security/OAuth2AccessTokenInterceptorTests.java
  11. 2
      spring-cloud-openfeign-dependencies/pom.xml

5
docs/src/main/asciidoc/_configprops.adoc

@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
|feign.autoconfiguration.jackson.enabled | `+++false+++` | If true, PageJacksonModule and SortJacksonModule bean will be provided for Jackson page decoding.
|feign.circuitbreaker.alphanumeric-ids.enabled | `+++false+++` | If true, Circuit Breaker ids will only contain alphanumeric characters to allow for configuration via configuration properties.
|feign.circuitbreaker.enabled | `+++false+++` | If true, an OpenFeign client will be wrapped with a Spring Cloud CircuitBreaker circuit breaker.
|feign.circuitbreaker.group.enabled | `+++false+++` | If true, an OpenFeign client will be wrapped with a Spring Cloud CircuitBreaker circuit breaker with with group.
|feign.circuitbreaker.group.enabled | `+++false+++` | If true, an OpenFeign client will be wrapped with a Spring Cloud CircuitBreaker circuit breaker with group.
|feign.client.config | |
|feign.client.decode-slash | `+++true+++` | Feign clients do not encode slash `/` characters by default. To change this behavior, set the `decodeSlash` to `false`.
|feign.client.default-config | `+++default+++` |
@ -31,8 +31,9 @@ @@ -31,8 +31,9 @@
|feign.httpclient.time-to-live | `+++900+++` |
|feign.httpclient.time-to-live-unit | |
|feign.metrics.enabled | `+++true+++` | Enables metrics capability for Feign.
|feign.oauth2.clientRegistrationId | | Provides a clientRegistrationId to be used with OAuth2.
|feign.oauth2.enabled | `+++false+++` | Enables feign interceptor for managing oauth2 access token.
|feign.oauth2.load-balanced | `+++false+++` | Enables load balancing for oauth2 access token provider.
|feign.okhttp.enabled | `+++false+++` | Enables the use of the OK HTTP Client by Feign.
|===
|===

16
docs/src/main/asciidoc/spring-cloud-openfeign.adoc

@ -807,16 +807,26 @@ OAuth2 support can be enabled by setting following flag: @@ -807,16 +807,26 @@ OAuth2 support can be enabled by setting following flag:
----
feign.oauth2.enabled=true
----
When the flag is set to true, and the oauth2 client context resource details are present, a bean of class `OAuth2FeignRequestInterceptor` is created. Before each request, the interceptor resolves the required access token and includes it as a header.
==== Deprecated OAuth2 Support
When the flag is set to `true`, and `spring-security-oauth2-autoconfigure` is present in the classpath and the oauth2 client context resource details are present and `OAuth2ClientContext` and `OAuth2ProtectedResourceDetails` beans are present, a bean of class `OAuth2FeignRequestInterceptor` is created. Before each request, the interceptor resolves the required access token and includes it as a header.
Sometimes, when load balancing is enabled for Feign clients, you may want to use load balancing for fetching access tokens, too. To do so, you should ensure that the load balancer is on the classpath (spring-cloud-starter-loadbalancer) and explicitly enable load balancing for OAuth2FeignRequestInterceptor by setting the following flag:
----
feign.oauth2.load-balanced=true
----
When the flag is set to true, and the oauth2 client context resource details are present, a bean of class `OAuth2AccessTokenInterceptor` is created. Before each request, the interceptor resolves the required access token and includes it as a header.
`OAuth2AccessTokenInterceptor` uses the `AuthorizedClientServiceOAuth2AuthorizedClientManager` to get `OAuth2AuthorizedClient` that holds an `OAuth2AccessToken`. If the user has specified an OAuth2 `clientRegistrationId` using the `spring.cloud.openfeign.oauth2.clientRegistrationId` property, it will be used to retrieve the token. If the token is not retrieved or the `clientRegistrationId` has not been specified, the `serviceId` retrieved from the `url` host segment will be used.
WARNING:: The OAuth2 support as described above is now deprecated since `spring-security-oauth2-autoconfigure` has reached end of life. Please use the mode described below instead.
==== Current OAuth2 Support
When the `feign.client.refresh-enabled` flag is set to true, and `spring-security-oauth2-client` is present in the classpath, a bean of class `OAuth2AccessTokenInterceptor` is created. Before each request, the interceptor resolves the required access token and includes it as a header.
`OAuth2AccessTokenInterceptor` uses the `OAuth2AuthorizedClientManager` to get `OAuth2AuthorizedClient` that holds an `OAuth2AccessToken`. If the user has specified an OAuth2 `clientRegistrationId` using the `feign.oauth2.clientRegistrationId` property, it will be used to retrieve the token. If the token is not retrieved or the `clientRegistrationId` has not been specified, the `serviceId` retrieved from the `url` host segment will be used.
TIP:: Using the `serviceId` as OAuth2 client registrationId is convenient for load-balanced Feign clients. For non-load-balanced ones, the property-based `clientRegistrationId` is a suitable approach.
TIP:: If you do not want to use the default setup for the `OAuth2AuthorizedClientManager`, you can just instantiate a bean of this type in your configuration.
=== Transform the load-balanced HTTP request
You can use the selected `ServiceInstance` to transform the load-balanced HTTP Request.

33
spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignAutoConfiguration.java

@ -75,6 +75,7 @@ import org.springframework.context.annotation.Configuration; @@ -75,6 +75,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.security.oauth2.client.AuthorizedClientServiceOAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
@ -352,7 +353,7 @@ public class FeignAutoConfiguration { @@ -352,7 +353,7 @@ public class FeignAutoConfiguration {
@ConditionalOnClass(OAuth2ClientContext.class)
@ConditionalOnProperty("feign.oauth2.enabled")
@Deprecated // spring-security-oauth2 reached EOL
protected static class Oauth2FeignConfiguration {
protected static class DeprecatedOauth2FeignConfiguration {
@ConditionalOnBean({ RetryLoadBalancerInterceptor.class, OAuth2ClientContext.class,
OAuth2ProtectedResourceDetails.class })
@ -374,20 +375,36 @@ public class FeignAutoConfiguration { @@ -374,20 +375,36 @@ public class FeignAutoConfiguration {
@Bean
@ConditionalOnMissingBean(OAuth2FeignRequestInterceptor.class)
@ConditionalOnBean({OAuth2ClientContext.class, OAuth2ProtectedResourceDetails.class})
@ConditionalOnBean({ OAuth2ClientContext.class, OAuth2ProtectedResourceDetails.class })
public RequestInterceptor oauth2FeignRequestInterceptor(OAuth2ClientContext oAuth2ClientContext,
OAuth2ProtectedResourceDetails resource, List<OAuth2FeignRequestInterceptorConfigurer> configurers) {
OAuth2ProtectedResourceDetails resource, List<OAuth2FeignRequestInterceptorConfigurer> configurers) {
return buildWithConfigurers(oAuth2ClientContext, resource, configurers);
}
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(OAuth2AuthorizedClientManager.class)
@ConditionalOnProperty("feign.oauth2.enabled")
protected static class Oauth2FeignConfiguration {
@Bean
@ConditionalOnBean({ OAuth2AuthorizedClientService.class, ClientRegistrationRepository.class })
@ConditionalOnMissingBean
OAuth2AuthorizedClientManager feignOAuth2AuthorizedClientManager(
ClientRegistrationRepository clientRegistrationRepository,
OAuth2AuthorizedClientService oAuth2AuthorizedClientService) {
return new AuthorizedClientServiceOAuth2AuthorizedClientManager(clientRegistrationRepository,
oAuth2AuthorizedClientService);
}
@Bean
@ConditionalOnBean(OAuth2AuthorizedClientManager.class)
public OAuth2AccessTokenInterceptor defaultOAuth2AccessTokenInterceptor(
@Value("${spring.cloud.openfeign.oauth2.clientRegistrationId:}") String clientRegistrationId,
OAuth2AuthorizedClientService oAuth2AuthorizedClientService,
ClientRegistrationRepository clientRegistrationRepository) {
return new OAuth2AccessTokenInterceptor(clientRegistrationId, oAuth2AuthorizedClientService,
clientRegistrationRepository);
@Value("${feign.oauth2.clientRegistrationId:}") String clientRegistrationId,
OAuth2AuthorizedClientManager oAuth2AuthorizedClientManager) {
return new OAuth2AccessTokenInterceptor(clientRegistrationId, oAuth2AuthorizedClientManager);
}
}

45
spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/security/OAuth2AccessTokenInterceptor.java

@ -27,26 +27,23 @@ import org.springframework.security.authentication.AnonymousAuthenticationToken; @@ -27,26 +27,23 @@ import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.AuthorizedClientServiceOAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizeRequest;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* A {@link RequestInterceptor} for OAuth2 Feign Requests. By default, it uses the
* {@link AuthorizedClientServiceOAuth2AuthorizedClientManager } to get
* {@link OAuth2AuthorizedClient } that holds an {@link OAuth2AccessToken }. If the user
* has specified an OAuth2 {@code clientId} using the
* {@code spring.cloud.openfeign.oauth2.clientId} property, it will be used to retrieve
* the token. If the token is not retrieved or the {@code clientId} has not been
* specified, the {@code serviceId} retrieved from the {@code url} host segment will be
* used. This approach is convenient for load-balanced Feign clients. For
* non-load-balanced ones, the property-based {@code clientId} is a suitable approach.
* {@link OAuth2AuthorizedClientManager } to get {@link OAuth2AuthorizedClient } that
* holds an {@link OAuth2AccessToken }. If the user has specified an OAuth2
* {@code clientRegistrationId} using the {@code feign.oauth2.clientRegistrationId}
* property, it will be used to retrieve the token. If the token is not retrieved or the
* {@code clientRegistrationId} has not been specified, the {@code serviceId} retrieved
* from the {@code url} host segment will be used. This approach is convenient for
* load-balanced Feign clients. For non-load-balanced ones, the property-based
* {@code clientRegistrationId} is a suitable approach.
*
* @author Dangzhicairang(小水牛)
* @author Olga Maciaszek-Sharma
@ -70,34 +67,26 @@ public class OAuth2AccessTokenInterceptor implements RequestInterceptor { @@ -70,34 +67,26 @@ public class OAuth2AccessTokenInterceptor implements RequestInterceptor {
private final String clientRegistrationId;
private OAuth2AuthorizedClientManager authorizedClientManager;
public void setAuthorizedClientManager(OAuth2AuthorizedClientManager authorizedClientManager) {
this.authorizedClientManager = authorizedClientManager;
}
private final OAuth2AuthorizedClientManager authorizedClientManager;
private static final Authentication ANONYMOUS_AUTHENTICATION = new AnonymousAuthenticationToken("anonymous",
"anonymousUser", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
public OAuth2AccessTokenInterceptor(OAuth2AuthorizedClientService oAuth2AuthorizedClientService,
ClientRegistrationRepository clientRegistrationRepository) {
this(null, oAuth2AuthorizedClientService, clientRegistrationRepository);
public OAuth2AccessTokenInterceptor(OAuth2AuthorizedClientManager oAuth2AuthorizedClientManager) {
this(null, oAuth2AuthorizedClientManager);
}
public OAuth2AccessTokenInterceptor(String clientRegistrationId,
OAuth2AuthorizedClientService oAuth2AuthorizedClientService,
ClientRegistrationRepository clientRegistrationRepository) {
this(BEARER, AUTHORIZATION, clientRegistrationId, oAuth2AuthorizedClientService, clientRegistrationRepository);
OAuth2AuthorizedClientManager oAuth2AuthorizedClientManager) {
this(BEARER, AUTHORIZATION, clientRegistrationId, oAuth2AuthorizedClientManager);
}
public OAuth2AccessTokenInterceptor(String tokenType, String header, String clientRegistrationId,
OAuth2AuthorizedClientService oAuth2AuthorizedClientService,
ClientRegistrationRepository clientRegistrationRepository) {
OAuth2AuthorizedClientManager oAuth2AuthorizedClientManager) {
this.tokenType = tokenType;
this.header = header;
this.clientRegistrationId = clientRegistrationId;
this.authorizedClientManager = new AuthorizedClientServiceOAuth2AuthorizedClientManager(
clientRegistrationRepository, oAuth2AuthorizedClientService);
this.authorizedClientManager = oAuth2AuthorizedClientManager;
}
@Override
@ -144,9 +133,9 @@ public class OAuth2AccessTokenInterceptor implements RequestInterceptor { @@ -144,9 +133,9 @@ public class OAuth2AccessTokenInterceptor implements RequestInterceptor {
private static String getServiceId(RequestTemplate template) {
Target<?> feignTarget = template.feignTarget();
Assert.notNull(feignTarget, "feignTarget may not be null");
Assert.notNull(feignTarget, "FeignTarget may not be null.");
String url = feignTarget.url();
Assert.hasLength(url, "url may not be empty");
Assert.hasLength(url, "Url may not be empty.");
final URI originalUri = URI.create(url);
return originalUri.getHost();
}

1
spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/security/OAuth2FeignRequestInterceptor.java

@ -42,6 +42,7 @@ import org.springframework.security.oauth2.common.OAuth2AccessToken; @@ -42,6 +42,7 @@ import org.springframework.security.oauth2.common.OAuth2AccessToken;
* @author Joao Pedro Evangelista
* @author Tim Ysewyn
* @since 3.0.0
* @deprecated in favour of {@link OAuth2AccessTokenInterceptor}
*/
@Deprecated // spring-security-oauth2 reached EOL
public class OAuth2FeignRequestInterceptor implements RequestInterceptor {

2
spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/security/OAuth2FeignRequestInterceptorBuilder.java

@ -39,7 +39,9 @@ import org.springframework.security.oauth2.client.token.grant.password.ResourceO @@ -39,7 +39,9 @@ import org.springframework.security.oauth2.client.token.grant.password.ResourceO
*
* @author Wojciech Mąka
* @since 3.1.1
* @deprecated since spring-security-oauth2 reached EOL
*/
@Deprecated // spring-security-oauth2 reached EOL
public class OAuth2FeignRequestInterceptorBuilder {
private AccessTokenProvider accessTokenProvider;

2
spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/security/OAuth2FeignRequestInterceptorConfigurer.java

@ -26,7 +26,9 @@ import org.springframework.security.oauth2.client.token.AccessTokenProvider; @@ -26,7 +26,9 @@ import org.springframework.security.oauth2.client.token.AccessTokenProvider;
*
* @author Wojciech Mąka
* @since 3.1.1
* @deprecated since spring-security-oauth2 reached EOL
*/
@Deprecated // spring-security-oauth2 reached EOL
@FunctionalInterface
public interface OAuth2FeignRequestInterceptorConfigurer {

6
spring-cloud-openfeign-core/src/main/resources/META-INF/additional-spring-configuration-metadata.json

@ -23,7 +23,7 @@ @@ -23,7 +23,7 @@
{
"name": "feign.circuitbreaker.group.enabled",
"type": "java.lang.Boolean",
"description": "If true, an OpenFeign client will be wrapped with a Spring Cloud CircuitBreaker circuit breaker with with group.",
"description": "If true, an OpenFeign client will be wrapped with a Spring Cloud CircuitBreaker circuit breaker with group.",
"defaultValue": "false"
},
{
@ -81,9 +81,9 @@ @@ -81,9 +81,9 @@
"defaultValue": "false"
},
{
"name": "feign.oauth2.registrationClientId",
"name": "feign.oauth2.clientRegistrationId",
"type": "java.lang.String",
"description": "Provides a clientId to be used with OAuth2.",
"description": "Provides a clientRegistrationId to be used with OAuth2.",
"defaultValue": ""
}
]

31
spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/FeignAutoConfigurationTests.java

@ -127,19 +127,17 @@ class FeignAutoConfigurationTests { @@ -127,19 +127,17 @@ class FeignAutoConfigurationTests {
@Test
void shouldInstantiateFeignOAuth2FeignRequestInterceptorWithCustomAccessTokenProviderInterceptor() {
runner.withPropertyValues("feign.oauth2.enabled=true")
.withBean(MockOAuth2ClientContext.class, "token")
.withBean(BaseOAuth2ProtectedResourceDetails.class)
.withBean(CustomOAuth2FeignRequestInterceptorConfigurer.class).run(ctx -> {
assertOauth2FeignRequestInterceptorExists(ctx);
assertAccessTokenProviderInterceptorExists(ctx, BasicAuthenticationInterceptor.class);
});
runner.withPropertyValues("feign.oauth2.enabled=true").withBean(MockOAuth2ClientContext.class, "token")
.withBean(BaseOAuth2ProtectedResourceDetails.class)
.withBean(CustomOAuth2FeignRequestInterceptorConfigurer.class).run(ctx -> {
assertOauth2FeignRequestInterceptorExists(ctx);
assertAccessTokenProviderInterceptorExists(ctx, BasicAuthenticationInterceptor.class);
});
}
@Test
void shouldInstantiateFeignOAuth2FeignRequestInterceptor() {
runner.withPropertyValues("spring.cloud.openfeign.oauth2.enabled=true",
"spring.cloud.openfeign.oauth2.clientRegistrationId=feign-client")
runner.withPropertyValues("feign.oauth2.enabled=true", "feign.oauth2.clientRegistrationId=feign-client")
.withBean(OAuth2AuthorizedClientService.class, () -> mock(OAuth2AuthorizedClientService.class))
.withBean(ClientRegistrationRepository.class, () -> mock(ClientRegistrationRepository.class))
.run(ctx -> {
@ -154,21 +152,18 @@ class FeignAutoConfigurationTests { @@ -154,21 +152,18 @@ class FeignAutoConfigurationTests {
}
private void assertAccessTokenProviderInterceptorExists(ConfigurableApplicationContext ctx,
Class<? extends ClientHttpRequestInterceptor> clazz) {
Class<? extends ClientHttpRequestInterceptor> clazz) {
AssertableApplicationContext context = AssertableApplicationContext.get(() -> ctx);
assertThat(context).getBean(OAuth2FeignRequestInterceptor.class)
.extracting("accessTokenProvider")
assertThat(context).getBean(OAuth2FeignRequestInterceptor.class).extracting("accessTokenProvider")
.extracting("interceptors").asList().first().isInstanceOf(clazz);
}
private void assertAccessTokenProviderInterceptorNotExists(ConfigurableApplicationContext ctx,
Class<? extends ClientHttpRequestInterceptor> clazz) {
Class<? extends ClientHttpRequestInterceptor> clazz) {
AssertableApplicationContext context = AssertableApplicationContext.get(() -> ctx);
assertThat(context).getBean(OAuth2FeignRequestInterceptor.class)
.extracting("accessTokenProvider")
.extracting("interceptors").asList()
.filteredOn(obj -> clazz.isAssignableFrom(obj.getClass()))
.isEmpty();
assertThat(context).getBean(OAuth2FeignRequestInterceptor.class).extracting("accessTokenProvider")
.extracting("interceptors").asList().filteredOn(obj -> clazz.isAssignableFrom(obj.getClass()))
.isEmpty();
}
private void assertOauth2AccessTokenInterceptorExists(ConfigurableApplicationContext ctx) {

29
spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/security/OAuth2AccessTokenInterceptorTests.java

@ -27,9 +27,7 @@ import org.junit.jupiter.api.Test; @@ -27,9 +27,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.security.oauth2.client.OAuth2AuthorizeRequest;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
@ -49,12 +47,6 @@ import static org.mockito.Mockito.when; @@ -49,12 +47,6 @@ import static org.mockito.Mockito.when;
*/
class OAuth2AccessTokenInterceptorTests {
private final ClientRegistrationRepository mockClientRegistrationRepository = mock(
ClientRegistrationRepository.class);
private final OAuth2AuthorizedClientService mockOAuth2AuthorizedClientService = mock(
OAuth2AuthorizedClientService.class);
private final OAuth2AuthorizedClientManager mockOAuth2AuthorizedClientManager = mock(
OAuth2AuthorizedClientManager.class);
@ -74,10 +66,8 @@ class OAuth2AccessTokenInterceptorTests { @@ -74,10 +66,8 @@ class OAuth2AccessTokenInterceptorTests {
@Test
void shouldThrowExceptionWhenNoTokenAcquired() {
oAuth2AccessTokenInterceptor = new OAuth2AccessTokenInterceptor(mockOAuth2AuthorizedClientService,
mockClientRegistrationRepository);
oAuth2AccessTokenInterceptor = new OAuth2AccessTokenInterceptor(mockOAuth2AuthorizedClientManager);
when(mockOAuth2AuthorizedClientManager.authorize(any())).thenReturn(null);
oAuth2AccessTokenInterceptor.setAuthorizedClientManager(mockOAuth2AuthorizedClientManager);
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> oAuth2AccessTokenInterceptor.apply(requestTemplate))
@ -86,10 +76,10 @@ class OAuth2AccessTokenInterceptorTests { @@ -86,10 +76,10 @@ class OAuth2AccessTokenInterceptorTests {
@Test
void shouldAcquireValidToken() {
oAuth2AccessTokenInterceptor = new OAuth2AccessTokenInterceptor(mockOAuth2AuthorizedClientService,
mockClientRegistrationRepository);
when(mockOAuth2AuthorizedClientManager.authorize(any())).thenReturn(validTokenOAuth2AuthorizedClient());
oAuth2AccessTokenInterceptor.setAuthorizedClientManager(mockOAuth2AuthorizedClientManager);
oAuth2AccessTokenInterceptor = new OAuth2AccessTokenInterceptor(mockOAuth2AuthorizedClientManager);
when(mockOAuth2AuthorizedClientManager.authorize(
argThat((OAuth2AuthorizeRequest request) -> ("test").equals(request.getClientRegistrationId()))))
.thenReturn(validTokenOAuth2AuthorizedClient());
oAuth2AccessTokenInterceptor.apply(requestTemplate);
@ -98,12 +88,10 @@ class OAuth2AccessTokenInterceptorTests { @@ -98,12 +88,10 @@ class OAuth2AccessTokenInterceptorTests {
@Test
void shouldAcquireValidTokenFromServiceId() {
oAuth2AccessTokenInterceptor = new OAuth2AccessTokenInterceptor(mockOAuth2AuthorizedClientService,
mockClientRegistrationRepository);
when(mockOAuth2AuthorizedClientManager.authorize(
argThat((OAuth2AuthorizeRequest request) -> ("test").equals(request.getClientRegistrationId()))))
.thenReturn(validTokenOAuth2AuthorizedClient());
oAuth2AccessTokenInterceptor.setAuthorizedClientManager(mockOAuth2AuthorizedClientManager);
oAuth2AccessTokenInterceptor = new OAuth2AccessTokenInterceptor(mockOAuth2AuthorizedClientManager);
oAuth2AccessTokenInterceptor.apply(requestTemplate);
@ -111,13 +99,12 @@ class OAuth2AccessTokenInterceptorTests { @@ -111,13 +99,12 @@ class OAuth2AccessTokenInterceptorTests {
}
@Test
void shouldAcquireValidTokenFromSpecifiedClientId() {
void shouldAcquireValidTokenFromSpecifiedClientRegistrationId() {
oAuth2AccessTokenInterceptor = new OAuth2AccessTokenInterceptor(DEFAULT_CLIENT_REGISTRATION_ID,
mockOAuth2AuthorizedClientService, mockClientRegistrationRepository);
mockOAuth2AuthorizedClientManager);
when(mockOAuth2AuthorizedClientManager
.authorize(argThat((OAuth2AuthorizeRequest request) -> (DEFAULT_CLIENT_REGISTRATION_ID)
.equals(request.getClientRegistrationId())))).thenReturn(validTokenOAuth2AuthorizedClient());
oAuth2AccessTokenInterceptor.setAuthorizedClientManager(mockOAuth2AuthorizedClientManager);
oAuth2AccessTokenInterceptor.apply(requestTemplate);

2
spring-cloud-openfeign-dependencies/pom.xml

@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
<feign-form.version>3.8.0</feign-form.version>
<!-- Deprecated - reached EOL -->
<spring-security-oauth2-autoconfigure.version>2.5.2</spring-security-oauth2-autoconfigure.version>
<spring-security-oauth2-client.version>5.7.3</spring-security-oauth2-client.version>
</properties>
<dependencyManagement>
<dependencies>
@ -30,6 +31,7 @@ @@ -30,6 +31,7 @@
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
<version>${spring-security-oauth2-client.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>

Loading…
Cancel
Save