Spring Cloud Config启用https
前言
本篇主要整理spring cloud config如何开启https,以及微服务如何通过https去访问spring cloud config服务器上的配置文件。
项目工程
包含一个eurekaServer服务器,一个eurekaClient客户端,一个cloud config server配置中心。 ![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-696QCpo5-1638083983368)(/Users/yuanzhihao/Library/Application Support/typora-user-images/image-20211128135316146.png)]](https://img-blog.csdnimg.cn/b2eb172fccfc4b73879ab0e09f1c53ac.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5rSS6ISx55qE5pm66Zqc,size_20,color_FFFFFF,t_70,g_se,x_16)
springcloudconfig配置中心开启https
和之前整理的文章一样,配置中心其实就是一个spring boot的工程,只要添加ssl证书以及配置证书的账号密码这些信息就可以了,这边就简单梳理下。
首先生成spring cloud config配置中心的证书configServer.keystore,关于证书生成,可以看一下之前的文章。 
将证书放置到配置中心项目的resources目录下,并在配置文件中添加如下的配置: 
server:
port: 8888
ssl:
enabled: true
key-alias: configServer
key-store: classpath:configServer.keystore
key-store-type: JKS
key-store-password: 123456
spring cloud config配置中心同时也作为一个eurekaClient,需要通过https的方式注册到eureka服务器上。
添加信任库trustStore.keystore文件到项目的resource目录,信任库中包含了eureka的证书 
在配置文件中设置信任库信息
server:
port: 8888
ssl:
trust-store: classpath:trustStore.keystore
trust-store-type: JKS
trust-store-password: 123456
注入一个DiscoveryClientOptionalArgs的bean,同时设置其SSLContext的trustStore属性为设置的信任库的信息
@Configuration
public class SSLContextConfig {
@Value("${server.ssl.trust-store}")
private String trustStorePath;
@Value("${server.ssl.trust-store-password}")
private String trustStorePassword;
@Bean
public SSLContext sslContext() throws Exception {
return SSLContextBuilder.
create().
loadTrustMaterial(ResourceUtils.getFile(trustStorePath), trustStorePassword.toCharArray()).
build();
}
}
@Bean
public DiscoveryClient.DiscoveryClientOptionalArgs discoveryClientOptionalArgs(SSLContext sslContext) {
DiscoveryClient.DiscoveryClientOptionalArgs discoveryClientOptionalArgs = new DiscoveryClient.DiscoveryClientOptionalArgs();
discoveryClientOptionalArgs.setSSLContext(sslContext);
return discoveryClientOptionalArgs;
}
启动spring cloud config配置中心,发现configServer已经注册到了eureka服务器上: 
客户端通过https去读取spring cloud config配置中心的配置文件
客户端通过https去访问spring cloud config配置中心首选需要将配置中心的证书加到信任库文件中,具体如何导入证书可以参考我之前的博客。 
将配置文件中spring.cloud.config.uri修改为https
spring:
application:
name: eureka-client1
cloud:
config:
name: config
uri: https://localhost:8888
profile: default
之后需要重新覆盖一下客户端与配置中心之间调用使用的RestTemplate对象,一开始我没有覆盖,导致一直是调用错误。
这边具体参考了一篇博客和spring cloud config官方文档,参考链接我放在了最后~
具体操作如下:
首先,需要自己实现一个ConfigServicePropertySourceLocator配置类,我理解是覆盖spring cloud config默认的配置
@Configuration
public class CustomConfigServiceBootstrapConfiguration {
@Autowired
private ConfigClientProperties clientProperties;
@Value("${server.ssl.trust-store}")
private String trustStorePath;
@Value("${server.ssl.trust-store-password}")
private String trustStorePassword;
@Bean
public ConfigServicePropertySourceLocator configServicePropertySourceLocator() throws Exception {
ConfigServicePropertySourceLocator configServicePropertySourceLocator = new ConfigServicePropertySourceLocator(clientProperties);
SSLContext sslContext = SSLContextBuilder.create().loadTrustMaterial(ResourceUtils.getFile(trustStorePath), trustStorePassword.toCharArray()).build();
CloseableHttpClient build = HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(build);
RestTemplate customRestTemplate = new RestTemplate(factory);
configServicePropertySourceLocator.setRestTemplate(customRestTemplate);
return configServicePropertySourceLocator;
}
}
之后,需要在resources/META-INF目录下,创建一个名字为spring.factories的配置文件,添加如下配置来让spring加载我们自定义的配置类 
spring.factories
org.springframework.cloud.bootstrap.BootstrapConfiguration = com.yzh.client1.config.CustomConfigServiceBootstrapConfiguration
启动客户端发现已经正常运行了,也可以访问到spring cloud config配置中心的配置文件,spring cloud config启用https OK!  
参考以及源码
参考地址:
- https://cloud.spring.io/spring-cloud-config/reference/html/#custom-rest-template
- https://piotrminkowski.com/2019/12/03/secure-spring-cloud-config/
源码地址:https://github.com/yzh19961031/SpringCloudDemo
|