Skip to content

Instantly share code, notes, and snippets.

@arony
Created April 4, 2018 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arony/8235b591e53eb55d5d2c5694edb9ecef to your computer and use it in GitHub Desktop.
Save arony/8235b591e53eb55d5d2c5694edb9ecef to your computer and use it in GitHub Desktop.
package com.nova.stats.relay;
import feign.RequestInterceptor;
import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateFactory;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.web.client.RestTemplate;
@Configuration
public class TokenRelayAutoConfiguration {
@Configuration
public static class RestTemplateConfiguration {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Configuration
public static class SecureRestTemplateConfiguration {
@Bean
@LoadBalanced
OAuth2RestTemplate restTemplate(UserInfoRestTemplateFactory factory) {
return factory.getUserInfoRestTemplate();
}
}
@Configuration
public static class FeignAutoConfiguration {
@Bean
RequestInterceptor requestInterceptor(OAuth2ClientContext clientContext) {
return requestTemplate ->
requestTemplate.header(HttpHeaders.AUTHORIZATION,
clientContext.getAccessToken().getTokenType() +
' ' + clientContext.getAccessToken().getValue());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment