Commit f36d948c authored by Yiming Liu's avatar Yiming Liu

Add auth header in client

parent 4054304f
...@@ -26,5 +26,10 @@ ...@@ -26,5 +26,10 @@
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>
...@@ -2,6 +2,7 @@ package com.ctrip.apollo.util.http; ...@@ -2,6 +2,7 @@ package com.ctrip.apollo.util.http;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.io.BaseEncoding;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.ctrip.apollo.util.ConfigUtil; import com.ctrip.apollo.util.ConfigUtil;
...@@ -12,6 +13,7 @@ import org.unidal.lookup.annotation.Named; ...@@ -12,6 +13,7 @@ import org.unidal.lookup.annotation.Named;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
...@@ -24,9 +26,15 @@ public class HttpUtil { ...@@ -24,9 +26,15 @@ public class HttpUtil {
@Inject @Inject
private ConfigUtil m_configUtil; private ConfigUtil m_configUtil;
private Gson gson; private Gson gson;
private String basicAuth;
public HttpUtil() { public HttpUtil() {
gson = new Gson(); gson = new Gson();
try {
basicAuth = "Basic " + BaseEncoding.base64().encode("".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} }
/** /**
...@@ -74,6 +82,7 @@ public class HttpUtil { ...@@ -74,6 +82,7 @@ public class HttpUtil {
HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection(); HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection();
conn.setRequestMethod("GET"); conn.setRequestMethod("GET");
conn.setRequestProperty ("Authorization", basicAuth);
int connectTimeout = httpRequest.getConnectTimeout(); int connectTimeout = httpRequest.getConnectTimeout();
if (connectTimeout < 0) { if (connectTimeout < 0) {
...@@ -102,9 +111,8 @@ public class HttpUtil { ...@@ -102,9 +111,8 @@ public class HttpUtil {
return new HttpResponse<>(statusCode, null); return new HttpResponse<>(statusCode, null);
} }
throw new RuntimeException( throw new RuntimeException(String.format("Get operation failed for %s, status code - %d",
String.format("Get operation failed for %s, status code - %d", httpRequest.getUrl(), httpRequest.getUrl(), statusCode));
statusCode));
} catch (Throwable ex) { } catch (Throwable ex) {
throw new RuntimeException("Could not complete get operation", ex); throw new RuntimeException("Could not complete get operation", ex);
...@@ -113,7 +121,7 @@ public class HttpUtil { ...@@ -113,7 +121,7 @@ public class HttpUtil {
try { try {
is.close(); is.close();
} catch (IOException ex) { } catch (IOException ex) {
//ignore // ignore
} }
} }
} }
......
...@@ -6,9 +6,11 @@ import org.springframework.context.annotation.ComponentScan.Filter; ...@@ -6,9 +6,11 @@ import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.FilterType;
import com.ctrip.apollo.common.controller.WebSecurityConfig;
@Configuration @Configuration
@ComponentScan(excludeFilters = {@Filter(type = FilterType.ASSIGNABLE_TYPE, value = { @ComponentScan(excludeFilters = {@Filter(type = FilterType.ASSIGNABLE_TYPE, value = {
SampleConfigServiceApplication.class, ConfigServiceApplication.class})}) SampleConfigServiceApplication.class, ConfigServiceApplication.class, WebSecurityConfig.class})})
@EnableAutoConfiguration @EnableAutoConfiguration
public class ConfigServiceTestConfiguration { public class ConfigServiceTestConfiguration {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment