Im Using Spring Boot 2.7.2 together with Spring Cloud 2021.0. I have multiple OAuth2 providers:
spring:
security:
oauth2:
client:
provider:
provider-one:
authorization-uri: https://provider-one/oauth/authorize
token-uri: https://provider-one/oauth/token
provider-two:
authorization-uri: https://provider-two/oauth/authorize
token-uri: https://provider-two/oauth/token
registration:
client-one:
provider: provider-one
client-id: client-one
authorization-grant-type: client_credentials
client-two:
provider: provider-two
client-id: client-two
authorization-grant-type: client_credentials
We can define the client registration id for spring cloud openfeign:
spring:
cloud:
openfeign:
oauth2:
enabled: true
client-registration-id: client-one
But what do I do if I need FeignClientA to get a token from provider-one and I need FeignClientB to get a token from provider-two? Is there a configurative way to
achieve that or do I need to set one interceptor for each feign client myself?
Application properties:
Mind:
registration-idand notclient-idAnd then Feign clients with a request interceptor in conf:
Edit: Migrating from
@FeignClientto@HttpExchangeproxy withRestClientIt seems that
spring-cloud-openfeignis entering "maintenance" mode. The references I could find about that are an answer from @OlgaMaciaszek and this issue on the Github repo.I explored a bit around the alternative recommended by Olga and liked it enough to add some experimental support for Spring
@HttpExchangefactories to this starter I maintain (but be aware that it works only with Boot3.2.2):If you don't want to use "my" starter, you can have a look at
BearerTokenAuthenticationInterceptorandAuthorizedClientBearerProviderfor theRequestInterceptorimpl and then atSpringAddonsRestClientSupportfor theRestClientconfiguration and@Serviceproxying.