
文章共 591字,阅读大约需要 2分钟 !
概 述
在我的前文《Eureka Server 开启Spring Security Basic认证》中已经给 Eureka Server 开启了最基本的鉴权措施,本文则让 HTTPS加持于 Eureka Server,让安全措施来的更彻底一点。
注: 本文首发于 My Personal Blog:CodeSheep·程序羊,欢迎光临 小站
证书准备
这里使用 JDK自带的 keytools 来创建证书
-
Server 端证书生成
keytool -genkeypair -alias server -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore codesheepserver.p12 -validity 3800
过程如下:

-
Client 端证书生成
keytool -genkeypair -alias client -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore codesheepclient.p12 -validity 3800
过程类似,就不再截图了
-
分别导出 server端和 client端的 p12证书
keytool -export -alias server -file codesheepserver.crt --keystore codesheepserver.p12 会要求你输入密码

keytool -export -alias client -file codesheepclient.crt --keystore codesheepclient.p12
导出的证书在此:

-
配置 Client端信任 Server端的证书
keytool -import -alias server -file codesheepserver.crt -keystore codesheepclient.p12
过程如下:

-
配置 Server端信任 Client端的证书
keytool -import -alias client -file codesheepclient.crt -keystore codesheepserver.p12
过程与上面类似,也不截图展示了
证书文件准备妥当之后,接下来进行项目代码级别的配置
Eureka Server SSL配置
我们需要在 Eureka Server的 Spring Boot项目中的 application.yml配置文件里将上文中生成的证书配到项目中去,即下面这段配置中与 server.ssl相关的部分:
1server: 2 port: 1111 3 ssl: 4 enabled: true 5 key-store: classpath:codesheepserver.p12 6 key-store-password: codesheep.cn 7 key-store-type: PKCS12 8 key-alias: server 9 10eureka: 11 instance: 12 hostname: localhost 13 securePort: 1111 14 securePortEnabled: true 15 nonSecurePortEnabled: false 16 client: 17 registerWithEureka: false 18 fetchRegistry: false
Eureka Client SSL配置
类似地,我们也在 Eureka Client的 Spring Boot项目中的 application.yml配置文件里将上文中生成的证书配到项目中去:
1server: 2 port: 1112 3spring: 4 application: 5 name: eureka-client 6eureka: 7 client: 8 securePortEnabled: true 9 serviceUrl: 10 defaultZone: https://localhost:1111/eureka/ 11ssl: 12 key-store: codesheepclient.p12 13 key-store-password: codesheep.cn
但注意此处的 ssl.key-store 和 ssl.key-store-password只是我们自定义的属性,我们需要结合自己编写的 ssl配置类 EurekaClientHttpsCfg来进行使用,代码如下:
1@Configuration 2public class EurekaClientHttpsCfg { 3 4 @Value("${ssl.key-store}") 5 String keyStoreFileName; 6 7 @Value("${ssl.key-store-password}") 8 String keyStorePassword; 9 10 @Bean 11 public DiscoveryClient.DiscoveryClientOptionalArgs discoveryClientOptionalArgs() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException { 12 EurekaJerseyClientImpl.EurekaJerseyClientBuilder builder = new EurekaJerseyClientImpl.EurekaJerseyClientBuilder(); 13 builder.withClientName("eureka-client"); 14 SSLContext sslContext = new SSLContextBuilder() 15 .loadTrustMaterial( 16 this.getClass().getClassLoader().getResource(keyStoreFileName),keyStorePassword.toCharArray() 17 ) 18 .build(); 19 builder.withCustomSSL(sslContext); 20 21 builder.withMaxTotalConnections(10); 22 builder.withMaxConnectionsPerHost(10); 23 24 DiscoveryClient.DiscoveryClientOptionalArgs args = new DiscoveryClient.DiscoveryClientOptionalArgs(); 25 args.setEurekaJerseyClient(builder.build()); 26 return args; 27 } 28}
这段代码的主要意图就是通过设置一个 SSLContext用于 Eureka Client访问 Eureka Server。
实验验证
- 启动 Eureka Server,由于其开启了 https访问,因此浏览器以非 https方式访问时就不通了

浏览器必须以 https方式访问注册中心方可:

- 启动 Eureka Client后,由于其已经加入了对 https的配置,因此可以验证通过并且注册到 Eureka Server注册中心:

如此一番实践下来,微服务注册中心的安全性就更进了一步。
后 记
由于能力有限,若有错误或者不当之处,还请大家批评指正,一起学习交流!
- My Personal Blog:CodeSheep 程序羊
- 我的半年技术博客之路