Commit d763b22a by lepdou

Merge pull request #114 from yiming187/auth_update

Add auth header
parents e79343cf c7e30148
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.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.google.common.io.BaseEncoding;
@Component
public class RestTemplateFactory implements FactoryBean<RestTemplate>, InitializingBean {
private RestTemplate restTemplate;
public RestTemplate getObject() {
......@@ -28,12 +36,18 @@ public class RestTemplateFactory implements FactoryBean<RestTemplate>, Initializ
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();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("apollo", ""));
CloseableHttpClient httpClient =
HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider)
.setDefaultHeaders(defaultHeaders).build();
restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));
}
......
......@@ -46,11 +46,7 @@ public class AppService {
}
public AppDTO save(AppDTO app) {
String createBy = app.getOwnerName();
try {
app.setDataChangeCreatedBy(createBy);
app.setDataChangeCreatedTime(new Date());
app.setDataChangeLastModifiedBy(createBy);
return appAPI.save(Env.DEV, app);
} catch (Exception 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