Skip to content

Instantly share code, notes, and snippets.

@phdd
Last active September 10, 2015 10:28
Show Gist options
  • Save phdd/a82b26be7dc572606f14 to your computer and use it in GitHub Desktop.
Save phdd/a82b26be7dc572606f14 to your computer and use it in GitHub Desktop.
public class OAuth2ServiceRequestInterceptor implements RequestInterceptor {
OAuth2AccessToken token;
OAuth2RestTemplate serviceTemplate;
public void setServiceTemplate(OAuth2RestTemplate serviceTemplate) {
this.serviceTemplate = serviceTemplate;
}
@Override public void apply(RequestTemplate template) {
injectAccessTokenInto(template);
}
void injectAccessTokenInto(RequestTemplate template) {
String authHeaderValue = String.format("Bearer %s", accessToken());
template.header(HttpHeaders.AUTHORIZATION, authHeaderValue);
}
OAuth2AccessToken accessToken() {
if (token == null || token.isExpired()) {
token = serviceTemplate.getAccessToken();
}
return token;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment