过早客
  • 首页
  • 节点
  • 成员
  • 广告投放
  • 登录
  • 注册

Eureka Server启用 https服务指北

IT技术 • CodeSheep • 发表于 6 年前

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相关的部分:

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-store 和 ssl.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方式访问时就不通了

非 https的方式是无法访问注册中心的

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

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

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

服务已经注册上来

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


后 记

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

  • My Personal Blog:CodeSheep 程序羊
  • 我的半年技术博客之路


加入收藏 新浪微博 分享到微信 ❤赞 1225 次点击 1 人赞 1 人收藏

打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮

暂无回复,这事你怎么看
请绑定手机号后,再发言,点击此处
Guozaoke.com—源自武汉的高端交流分享社区
相关主题
出akamai数据
根据 GitHub 个人贡献图生成贪吃蛇游戏,有点意思
哪个ai可以结合新闻中的图片和视频帮忙生成指定尺寸的组合图片呀?
做了个世界有趣街景网站,一刷就上瘾
小程序备案要多久
你们现在写游戏,还用unity吗?
感觉chatgpt还是比deepseek和grok强很多
能纯手搓一个全流程神经网络程序,在本版什么水平?
AI还是欠火候
新上线的flux 站点

过早客微信公众号:guozaoke • 过早客新浪微博:@过早客 • 广告投放合作微信:fullygroup50 鄂ICP备2021016276号-2 • 鄂公网安备42018502001446号