From 41db12cf7c3d7585e4810f64580224bf6d3c2f27 Mon Sep 17 00:00:00 2001 From: Marcello de Sales Date: Thu, 11 Aug 2016 01:07:13 -0700 Subject: [PATCH] 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 --- .../config/client/ConfigClientProperties.java | 16 ++++++++++ .../ConfigServicePropertySourceLocator.java | 31 ++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) 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); + } + } }