Eureka Server启用 https服务指北

Stella981
• 阅读 779

Eureka Server启用 https服务指北

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

过程如下:

Eureka Server启用 https服务指北

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

Eureka Server启用 https服务指北

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

导出的证书在此:

Eureka Server启用 https服务指北

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

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

过程如下:

Eureka Server启用 https服务指北

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

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

过程与上面类似,也不截图展示了

证书文件准备妥当之后,接下来进行项目代码级别的配置


Eureka Server SSL配置

我们需要在 Eureka Server的 Spring Boot项目中的 application.yml配置文件里将上文中生成的证书配到项目中去,即下面这段配置中与 server.ssl相关的部分:

server:
  port: 1111
  ssl:
    enabled: true
    key-store: classpath:codesheepserver.p12
    key-store-password: codesheep.cn
    key-store-type: PKCS12
    key-alias: server

eureka:
  instance:
    hostname: localhost
    securePort: 1111
    securePortEnabled: true
    nonSecurePortEnabled: false
  client:
    registerWithEureka: false
    fetchRegistry: false

Eureka Client SSL配置

类似地,我们也在 Eureka Client的 Spring Boot项目中的 application.yml配置文件里将上文中生成的证书配到项目中去:

server:
  port: 1112
spring:
  application:
    name: eureka-client
eureka:
  client:
    securePortEnabled: true
    serviceUrl:
      defaultZone: https://localhost:1111/eureka/
ssl:
  key-store: codesheepclient.p12
  key-store-password: codesheep.cn

但注意此处的 ssl.key-storessl.key-store-password只是我们自定义的属性,我们需要结合自己编写的 ssl配置类 EurekaClientHttpsCfg来进行使用,代码如下:

@Configuration
public class EurekaClientHttpsCfg {

    @Value("${ssl.key-store}")
    String keyStoreFileName;

    @Value("${ssl.key-store-password}")
    String keyStorePassword;

    @Bean
    public DiscoveryClient.DiscoveryClientOptionalArgs discoveryClientOptionalArgs() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException {
        EurekaJerseyClientImpl.EurekaJerseyClientBuilder builder = new EurekaJerseyClientImpl.EurekaJerseyClientBuilder();
        builder.withClientName("eureka-client");
        SSLContext sslContext = new SSLContextBuilder()
                .loadTrustMaterial(
                        this.getClass().getClassLoader().getResource(keyStoreFileName),keyStorePassword.toCharArray()
                )
                .build();
        builder.withCustomSSL(sslContext);

        builder.withMaxTotalConnections(10);
        builder.withMaxConnectionsPerHost(10);

        DiscoveryClient.DiscoveryClientOptionalArgs args = new DiscoveryClient.DiscoveryClientOptionalArgs();
        args.setEurekaJerseyClient(builder.build());
        return args;
    }
}

这段代码的主要意图就是通过设置一个 SSLContext用于 Eureka Client访问 Eureka Server。


实验验证

  • 启动 Eureka Server,由于其开启了 https访问,因此浏览器以非 https方式访问时就不通了

Eureka Server启用 https服务指北

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

Eureka Server启用 https服务指北

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

Eureka Server启用 https服务指北

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


后 记

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



点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
2年前
Redis 哈希结构内存模型剖析
!Profile(https://uploadimages.jianshu.io/upload_images/9824247af07824cf5595df9.png?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)本文共1231字,阅读大约需要5分钟!概述
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Spring Boot 集成 MyBatis和 SQL Server实践
!Profile(https://uploadimages.jianshu.io/upload_images/9824247e4105fb3034464fa.jpg?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)文章共509字,阅读大约需要2分钟!概述
Stella981 Stella981
2年前
Eureka Server 开启Spring Security Basic认证
!Desktop(https://uploadimages.jianshu.io/upload_images/98242475ce94f98ae00f42f.jpg?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)文章共503字,阅读大约需要2分钟!概述
Stella981 Stella981
2年前
EVCache缓存在 Spring Boot中的实战
!Profile(https://uploadimages.jianshu.io/upload_images/98242471808cf037d128163.jpg?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)文章共727字,阅读大约需要2分钟!概述
Stella981 Stella981
2年前
Spring Cloud Eureka Server高可用之:在线扩容
!Profile(https://uploadimages.jianshu.io/upload_images/9824247c5fa3fe2a839cb4f.jpg?imageMogr2/autoorient/strip%7CimageView2/2/w/1240)本文共1591字,阅读大约需要6分钟!概述
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这