提交 2d0b0e8a 编写于 作者: J Jason Song

Merge pull request #109 from yiming187/rest_factory

Support auth in RestTemplateFactory
......@@ -17,7 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.service.AdminService;
import com.ctrip.apollo.biz.service.AppService;
import com.ctrip.apollo.common.controller.ActiveUser;
import com.ctrip.apollo.common.auth.ActiveUser;
import com.ctrip.apollo.common.utils.BeanUtils;
import com.ctrip.apollo.core.dto.AppDTO;
import com.ctrip.apollo.core.exception.NotFoundException;
......
......@@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.service.ClusterService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.common.controller.ActiveUser;
import com.ctrip.apollo.common.auth.ActiveUser;
import com.ctrip.apollo.common.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.exception.NotFoundException;
......
......@@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.service.ItemService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.common.controller.ActiveUser;
import com.ctrip.apollo.common.auth.ActiveUser;
import com.ctrip.apollo.common.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.exception.NotFoundException;
......
......@@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.service.ItemSetService;
import com.ctrip.apollo.common.controller.ActiveUser;
import com.ctrip.apollo.common.auth.ActiveUser;
import com.ctrip.apollo.core.dto.ItemChangeSets;
@RestController
......
......@@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.service.NamespaceService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.common.controller.ActiveUser;
import com.ctrip.apollo.common.auth.ActiveUser;
import com.ctrip.apollo.common.utils.BeanUtils;
import com.ctrip.apollo.core.dto.NamespaceDTO;
import com.ctrip.apollo.core.exception.NotFoundException;
......
......@@ -14,7 +14,7 @@ import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.biz.service.ReleaseService;
import com.ctrip.apollo.biz.service.ViewService;
import com.ctrip.apollo.common.controller.ActiveUser;
import com.ctrip.apollo.common.auth.ActiveUser;
import com.ctrip.apollo.common.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ReleaseDTO;
import com.ctrip.apollo.core.exception.NotFoundException;
......
......@@ -2,6 +2,7 @@ package com.ctrip.apollo.util.http;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.io.BaseEncoding;
import com.google.gson.Gson;
import com.ctrip.apollo.util.ConfigUtil;
......@@ -12,6 +13,7 @@ import org.unidal.lookup.annotation.Named;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
......@@ -24,15 +26,21 @@ public class HttpUtil {
@Inject
private ConfigUtil m_configUtil;
private Gson gson;
private String basicAuth;
public HttpUtil() {
gson = new Gson();
try {
basicAuth = "Basic " + BaseEncoding.base64().encode("".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* Do get operation for the http request.
*
* @param httpRequest the request
* @param httpRequest the request
* @param responseType the response type
* @return the response
* @throws RuntimeException if any error happened or response code is neither 200 nor 304
......@@ -51,7 +59,7 @@ public class HttpUtil {
/**
* Do get operation for the http request.
*
* @param httpRequest the request
* @param httpRequest the request
* @param responseType the response type
* @return the response
* @throws RuntimeException if any error happened or response code is neither 200 nor 304
......@@ -68,13 +76,14 @@ public class HttpUtil {
}
private <T> HttpResponse<T> doGetWithSerializeFunction(HttpRequest httpRequest,
Function<String, T> serializeFunction) {
Function<String, T> serializeFunction) {
InputStream is = null;
try {
HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty ("Authorization", basicAuth);
int connectTimeout = httpRequest.getConnectTimeout();
if (connectTimeout < 0) {
connectTimeout = m_configUtil.getConnectTimeout();
......@@ -102,9 +111,8 @@ public class HttpUtil {
return new HttpResponse<>(statusCode, null);
}
throw new RuntimeException(
String.format("Get operation failed for %s, status code - %d", httpRequest.getUrl(),
statusCode));
throw new RuntimeException(String.format("Get operation failed for %s, status code - %d",
httpRequest.getUrl(), statusCode));
} catch (Throwable ex) {
throw new RuntimeException("Could not complete get operation", ex);
......@@ -113,7 +121,7 @@ public class HttpUtil {
try {
is.close();
} catch (IOException ex) {
//ignore
// ignore
}
}
}
......
......@@ -38,6 +38,10 @@
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
......
package com.ctrip.apollo.common.controller;
package com.ctrip.apollo.common.auth;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
......
package com.ctrip.apollo.common.auth;
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.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;
@Component
public class RestTemplateFactory implements FactoryBean<RestTemplate>, InitializingBean {
private RestTemplate restTemplate;
public RestTemplate getObject() {
return restTemplate;
}
public Class<RestTemplate> getObjectType() {
return RestTemplate.class;
}
public boolean isSingleton() {
return true;
}
public void afterPropertiesSet() {
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("apollo", ""));
CloseableHttpClient httpClient =
HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));
}
}
......@@ -67,6 +67,11 @@
</exclusions>
</dependency>
<!-- end of eureka -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -6,9 +6,11 @@ import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import com.ctrip.apollo.common.controller.WebSecurityConfig;
@Configuration
@ComponentScan(excludeFilters = {@Filter(type = FilterType.ASSIGNABLE_TYPE, value = {
SampleConfigServiceApplication.class, ConfigServiceApplication.class})})
SampleConfigServiceApplication.class, ConfigServiceApplication.class, WebSecurityConfig.class})})
@EnableAutoConfiguration
public class ConfigServiceTestConfiguration {
......
package com.ctrip.apollo.portal;
package com.ctrip.apollo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.system.ApplicationPidFileWriter;
......
package com.ctrip.apollo.portal.api;
import com.ctrip.apollo.core.enums.Env;
import com.ctrip.apollo.core.exception.ServiceException;
import com.ctrip.apollo.portal.service.ServiceLocator;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;
import com.ctrip.apollo.common.auth.RestTemplateFactory;
import com.ctrip.apollo.core.enums.Env;
import com.ctrip.apollo.core.exception.ServiceException;
import com.ctrip.apollo.portal.service.ServiceLocator;
public class API {
@Autowired
protected ServiceLocator serviceLocator;
protected RestTemplate restTemplate = new RestTemplate();
@Autowired
private RestTemplateFactory restTemplateFactory;
protected RestTemplate restTemplate;
@PostConstruct
private void postConstruct() {
restTemplate = restTemplateFactory.getObject();
}
public String getAdminServiceHost(Env env) {
// 本地测试用
......@@ -24,4 +35,5 @@ public class API {
}
return "";
}
}
......@@ -15,4 +15,4 @@ ctrip:
apollo:
portal:
env: dev
env: local
package com.ctrip.apollo;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class SamplePortalApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(SamplePortalApplication.class).run(args);
}
}
......@@ -4,6 +4,8 @@ import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.ctrip.apollo.PortalApplication;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = PortalApplication.class)
public abstract class AbstractPortalTest {
......
......@@ -4,11 +4,6 @@ server:
spring:
application:
name: apollo-portal
datasource:
url: jdbc:h2:mem:~/fxapolloportaldb
jpa:
hibernate:
naming_strategy: org.hibernate.cfg.EJB3NamingStrategy
logging:
level:
......@@ -20,4 +15,4 @@ ctrip:
apollo:
portal:
env: dev
env: local
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册