Commit c7e30148 by Yiming Liu

Add auth header

parent e79343cf
package com.ctrip.apollo.common.auth; package com.ctrip.apollo.common.auth;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.http.Header;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import com.google.common.io.BaseEncoding;
@Component @Component
public class RestTemplateFactory implements FactoryBean<RestTemplate>, InitializingBean { public class RestTemplateFactory implements FactoryBean<RestTemplate>, InitializingBean {
private RestTemplate restTemplate; private RestTemplate restTemplate;
public RestTemplate getObject() { public RestTemplate getObject() {
...@@ -28,12 +36,18 @@ public class RestTemplateFactory implements FactoryBean<RestTemplate>, Initializ ...@@ -28,12 +36,18 @@ public class RestTemplateFactory implements FactoryBean<RestTemplate>, Initializ
return true; return true;
} }
public void afterPropertiesSet() { public void afterPropertiesSet() throws UnsupportedEncodingException {
Collection<Header> defaultHeaders = new ArrayList<Header>();
Header header = new BasicHeader("Authorization",
"Basic " + BaseEncoding.base64().encode("apollo:".getBytes("UTF-8")));
defaultHeaders.add(header);
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("apollo", "")); new UsernamePasswordCredentials("apollo", ""));
CloseableHttpClient httpClient = CloseableHttpClient httpClient =
HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build(); HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider)
.setDefaultHeaders(defaultHeaders).build();
restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient)); restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));
} }
......
...@@ -46,11 +46,7 @@ public class AppService { ...@@ -46,11 +46,7 @@ public class AppService {
} }
public AppDTO save(AppDTO app) { public AppDTO save(AppDTO app) {
String createBy = app.getOwnerName();
try { try {
app.setDataChangeCreatedBy(createBy);
app.setDataChangeCreatedTime(new Date());
app.setDataChangeLastModifiedBy(createBy);
return appAPI.save(Env.DEV, app); return appAPI.save(Env.DEV, app);
} catch (Exception e) { } catch (Exception e) {
logger.error("oops! save app error. app id:{}", app.getAppId(), e); logger.error("oops! save app error. app id:{}", app.getAppId(), e);
......
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