Commit 429792a4 by Yiming Liu

Support auth in RestTemplateFactory

parent fd08a717
......@@ -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;
......
......@@ -39,6 +39,10 @@
<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>
</dependency>
......
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));
}
}
......@@ -12,7 +12,8 @@ public class API {
@Autowired
protected ServiceLocator serviceLocator;
protected RestTemplate restTemplate = new RestTemplate();
@Autowired
protected RestTemplate restTemplate;
public String getAdminServiceHost(Env env) {
// 本地测试用
......@@ -24,4 +25,5 @@ public class API {
}
return "";
}
}
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,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% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment