Alternative implementation of Source Locator: Authorization
This commit adds the Cloud Config Property "authorization", as the HTTP Authorization header to connect to the client. Users won't be able to set both the "password" and "authorization" at the same time. When set, the client will use the value to create a generic Authorization header with the value provided. Fixes gh-474
This commit is contained in:
committed by
Dave Syer
parent
e1fcd3000a
commit
41db12cf7c
@@ -90,6 +90,11 @@ public class ConfigClientProperties {
|
||||
*/
|
||||
private String token;
|
||||
|
||||
/**
|
||||
* Authorization token used by the client to connect to the server.
|
||||
*/
|
||||
private String authorization;
|
||||
|
||||
private ConfigClientProperties() {
|
||||
}
|
||||
|
||||
@@ -185,6 +190,16 @@ public class ConfigClientProperties {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public String getAuthorization() {
|
||||
return this.authorization;
|
||||
}
|
||||
|
||||
public void setAuthorization(String authorization) {
|
||||
this.authorization = authorization;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private String[] extractCredentials() {
|
||||
String[] result = new String[3];
|
||||
String uri = this.uri;
|
||||
@@ -286,6 +301,7 @@ public class ConfigClientProperties {
|
||||
+ this.profile + ", name=" + this.name + ", label="
|
||||
+ (this.label == null ? "" : this.label) + ", username=" + this.username
|
||||
+ ", password=" + this.password + ", uri=" + this.uri
|
||||
+ ", authorization=" + this.authorization
|
||||
+ ", discovery.enabled=" + this.discovery.enabled + ", failFast="
|
||||
+ this.failFast + ", token=" + this.token + "]";
|
||||
}
|
||||
|
||||
@@ -190,11 +190,23 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator
|
||||
requestFactory.setReadTimeout((60 * 1000 * 3) + 5000); //TODO 3m5s, make configurable?
|
||||
RestTemplate template = new RestTemplate(requestFactory);
|
||||
String password = client.getPassword();
|
||||
String authorization = client.getAuthorization();
|
||||
|
||||
if (password != null && authorization != null) {
|
||||
throw new IllegalStateException("You must set either 'password' or 'authorization'");
|
||||
}
|
||||
|
||||
if (password != null) {
|
||||
template.setInterceptors(Arrays
|
||||
.<ClientHttpRequestInterceptor> asList(new BasicAuthorizationInterceptor(
|
||||
client.getUsername(), password)));
|
||||
}
|
||||
} else
|
||||
if (authorization != null) {
|
||||
template.setInterceptors(Arrays
|
||||
.<ClientHttpRequestInterceptor>asList(new GenericAuthorization(
|
||||
authorization)));
|
||||
}
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
@@ -220,4 +232,21 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static class GenericAuthorization implements
|
||||
ClientHttpRequestInterceptor {
|
||||
|
||||
private final String authorizationToken;
|
||||
|
||||
public GenericAuthorization(String authorizationToken) {
|
||||
this.authorizationToken = (authorizationToken == null ? "" : authorizationToken);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
|
||||
throws IOException {
|
||||
request.getHeaders().add("Authorization", authorizationToken);
|
||||
return execution.execute(request, body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user