Eureka Server启用 https服务指北

New Mac Mini

文章共 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

过程如下:

Server 端证书生成过程

  • 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 会要求你输入密码

导出 server端的 p12证书

keytool -export -alias client -file codesheepclient.crt --keystore codesheepclient.p12

导出的证书在此:

导出 client端的 p12证书

  • 配置 Client端信任 Server端的证书

    keytool -import -alias server -file codesheepserver.crt -keystore codesheepclient.p12

过程如下:

配置 Client端信任 Server端的证书

  • 配置 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-storessl.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的方式是无法访问注册中心的

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

以 https方式访问注册中心方可

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

服务已经注册上来

如此一番实践下来,微服务注册中心的安全性就更进了一步。


后 记

由于能力有限,若有错误或者不当之处,还请大家批评指正,一起学习交流!



点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Redis 哈希结构内存模型剖析

!Profile(https://uploadimages.jianshu.io/upload_images/9824247af07824cf5595df9.png?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)本文共1231字,阅读大约需要5分钟!概述

Spring Boot 集成 MyBatis和 SQL Server实践

!Profile(https://uploadimages.jianshu.io/upload_images/9824247e4105fb3034464fa.jpg?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)文章共509字,阅读大约需要2分钟!概述

Eureka Server启用 https服务指北 - HelloWorld