vendredi 17 avril 2015

How to overwrite Spring Cloud OAuth2 client autoconfiguration?

We want to setup a microservice which provides a REST API so it is configured as a OAuth2 resource server. This service should also act as a OAuth2 client with the client credential grant. Here is the configuration:



spring.oauth2.client.id=clientCredentialsResource
spring.oauth2.client.accessTokenUri=http://localhost:9003/oauth/token
spring.oauth2.client.userAuthorizationUri=http://localhost:9003/oauth/authorize
spring.oauth2.client.grantType=client_credentials
spring.oauth2.client.clientId=<service-id>
spring.oauth2.client.clientSecret=<service-pw>


The resource server part works fine. For the client part we want to use Feign, Ribbon and Eureka:



@FeignClient("user")
public interface UserClient
{
@RequestMapping( method = RequestMethod.GET, value = "/user/{uid}")
Map<String, String> getUser(@PathVariable("uid") String uid);
}


Based on the gist in issue http://ift.tt/1PX8EJw I created a feign request intercepter which sets the access token from the autowired OAuth2RestOperations template in the feign request header



@Autowired
private OAuth2RestOperations restTemplate;

template.header(headerName, String.format("%s %s", tokenTypeName, restTemplate.getAccessToken().toString()));


But this gives me the error on calling the user service:



error="access_denied", error_description="Unable to obtain a new access token for resource 'clientCredentialsResource'. The provider manager is not configured to support it.


As I can see the OAuth2ClientAutoConfiguration creates always an instance of AuthorizationCodeResourceDetails for an web application but not the required ClientCredentialsResourceDetails which is only used for non-web applications. In the end the no access token privider is responsible for the resource details and the call failed in



AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:146)


I tried to overwrite the auto configuration but failed. Can somebody please give me a hint how to do it?


Aucun commentaire:

Enregistrer un commentaire