Commit 2d0b0e8a by Jason Song

Merge pull request #109 from yiming187/rest_factory

Support auth in RestTemplateFactory
parents fd08a717 f36d948c
...@@ -17,7 +17,7 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -17,7 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.App; import com.ctrip.apollo.biz.entity.App;
import com.ctrip.apollo.biz.service.AdminService; import com.ctrip.apollo.biz.service.AdminService;
import com.ctrip.apollo.biz.service.AppService; 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.common.utils.BeanUtils;
import com.ctrip.apollo.core.dto.AppDTO; import com.ctrip.apollo.core.dto.AppDTO;
import com.ctrip.apollo.core.exception.NotFoundException; import com.ctrip.apollo.core.exception.NotFoundException;
......
...@@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Cluster; import com.ctrip.apollo.biz.entity.Cluster;
import com.ctrip.apollo.biz.service.ClusterService; import com.ctrip.apollo.biz.service.ClusterService;
import com.ctrip.apollo.biz.service.ViewService; 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.common.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ClusterDTO; import com.ctrip.apollo.core.dto.ClusterDTO;
import com.ctrip.apollo.core.exception.NotFoundException; import com.ctrip.apollo.core.exception.NotFoundException;
......
...@@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Item; import com.ctrip.apollo.biz.entity.Item;
import com.ctrip.apollo.biz.service.ItemService; import com.ctrip.apollo.biz.service.ItemService;
import com.ctrip.apollo.biz.service.ViewService; 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.common.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ItemDTO; import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.exception.NotFoundException; import com.ctrip.apollo.core.exception.NotFoundException;
......
...@@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestMethod; ...@@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.service.ItemSetService; 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; import com.ctrip.apollo.core.dto.ItemChangeSets;
@RestController @RestController
......
...@@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -15,7 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.ctrip.apollo.biz.entity.Namespace; import com.ctrip.apollo.biz.entity.Namespace;
import com.ctrip.apollo.biz.service.NamespaceService; import com.ctrip.apollo.biz.service.NamespaceService;
import com.ctrip.apollo.biz.service.ViewService; 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.common.utils.BeanUtils;
import com.ctrip.apollo.core.dto.NamespaceDTO; import com.ctrip.apollo.core.dto.NamespaceDTO;
import com.ctrip.apollo.core.exception.NotFoundException; import com.ctrip.apollo.core.exception.NotFoundException;
......
...@@ -14,7 +14,7 @@ import com.ctrip.apollo.biz.entity.Release; ...@@ -14,7 +14,7 @@ import com.ctrip.apollo.biz.entity.Release;
import com.ctrip.apollo.biz.service.ConfigService; import com.ctrip.apollo.biz.service.ConfigService;
import com.ctrip.apollo.biz.service.ReleaseService; import com.ctrip.apollo.biz.service.ReleaseService;
import com.ctrip.apollo.biz.service.ViewService; 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.common.utils.BeanUtils;
import com.ctrip.apollo.core.dto.ReleaseDTO; import com.ctrip.apollo.core.dto.ReleaseDTO;
import com.ctrip.apollo.core.exception.NotFoundException; import com.ctrip.apollo.core.exception.NotFoundException;
......
...@@ -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
} }
} }
} }
......
...@@ -39,6 +39,10 @@ ...@@ -39,6 +39,10 @@
<artifactId>spring-data-commons</artifactId> <artifactId>spring-data-commons</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId> <groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>
</dependency> </dependency>
......
package com.ctrip.apollo.common.controller; package com.ctrip.apollo.common.auth;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType; 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 @@ ...@@ -67,6 +67,11 @@
</exclusions> </exclusions>
</dependency> </dependency>
<!-- end of eureka --> <!-- end of eureka -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
......
...@@ -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 {
......
package com.ctrip.apollo.portal; package com.ctrip.apollo;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.system.ApplicationPidFileWriter; import org.springframework.boot.actuate.system.ApplicationPidFileWriter;
......
package com.ctrip.apollo.portal.api; package com.ctrip.apollo.portal.api;
import com.ctrip.apollo.core.enums.Env; import javax.annotation.PostConstruct;
import com.ctrip.apollo.core.exception.ServiceException;
import com.ctrip.apollo.portal.service.ServiceLocator;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate; 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 { public class API {
@Autowired @Autowired
protected ServiceLocator serviceLocator; 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) { public String getAdminServiceHost(Env env) {
// 本地测试用 // 本地测试用
...@@ -24,4 +35,5 @@ public class API { ...@@ -24,4 +35,5 @@ public class API {
} }
return ""; return "";
} }
} }
...@@ -15,4 +15,4 @@ ctrip: ...@@ -15,4 +15,4 @@ ctrip:
apollo: apollo:
portal: 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; ...@@ -4,6 +4,8 @@ import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.ctrip.apollo.PortalApplication;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = PortalApplication.class) @SpringApplicationConfiguration(classes = PortalApplication.class)
public abstract class AbstractPortalTest { public abstract class AbstractPortalTest {
......
...@@ -4,11 +4,6 @@ server: ...@@ -4,11 +4,6 @@ server:
spring: spring:
application: application:
name: apollo-portal name: apollo-portal
datasource:
url: jdbc:h2:mem:~/fxapolloportaldb
jpa:
hibernate:
naming_strategy: org.hibernate.cfg.EJB3NamingStrategy
logging: logging:
level: level:
...@@ -20,4 +15,4 @@ ctrip: ...@@ -20,4 +15,4 @@ ctrip:
apollo: apollo:
portal: portal:
env: dev env: local
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