Last active
September 10, 2015 10:28
-
-
Save phdd/a82b26be7dc572606f14 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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