Auth0 Home Blog Docs

Audience not supported in Spring's @EnableOAuth2Client



We’re using Auth0 as Authorization Server in a micro services environment. Our batch application needs to connect to the main API through client credentials (machine-to-machine). We are using Spring’s RestTemplate and @EnableOAuth2Client to get an access_token and make calls to the Resource server. We’re using latest spring-boot (2.0.1.RELEASE). But, Auth0 needs the "audience"property filled and the ClientCredentialsResourceDetails() doesn’t have this property. So, I want to write an interceptor to add this property. Here’s the Auth0Config class:

public class OAuthConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(OAuthConfig.class);

    private String oAuth2ClientId;

    private String oAuth2ClientSecret;

    private String accessTokenUri;

    private String audience;

    public RestTemplate oAuthRestTemplate() {

        ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();

        OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails);

        final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
        interceptors.add(new AddAudienceInterceptor(audience));
        return restTemplate;

And here’s the interceptor:

public class AddAudienceInterceptor implements ClientHttpRequestInterceptor {
        private static final Logger LOGGER = LoggerFactory.getLogger(AddAudienceInterceptor.class);
        private String audience;
        public AddAudienceInterceptor(String audience) {
            this.audience = audience;
        public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
            if (bytes.length > 0) {
                ObjectMapper mapper = new ObjectMapper();
                Auth0ClientCredentials credentials = mapper.readValue(bytes, Auth0ClientCredentials.class);
                bytes = mapper.writeValueAsBytes(credentials);
            return clientHttpRequestExecution.execute(httpRequest, bytes);

The problem is that the interceptor works on the calls to the ResourceServer but the goal is to intercept the calls to the AuthorizationServer (Auth0 in my case).

The body of the call to the AuthorizationServer is currently:

{ grant_type: 'client_credentials',
  client_id: 'my_client_id',
  client_secret: 'my_client_secret' }

What I need to achieve is:

{ grant_type: 'client_credentials',
  client_id: 'my_client_id',
  client_secret: 'my_client_secret',
  audience: 'my_audience' }

Anyone any idea how I can achieve this? Thanks for you help!