diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigClientProperties.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigClientProperties.java index 843b6d6a..7f0ef768 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigClientProperties.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigClientProperties.java @@ -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 + "]"; } diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java index caf2b8f1..8cc7408b 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java @@ -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 . asList(new BasicAuthorizationInterceptor( client.getUsername(), password))); - } + } else + if (authorization != null) { + template.setInterceptors(Arrays + .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); + } + } }