转载

Eureka Server启用 https服务指北

Eureka Server 开启 https服务让安全性更上一层楼

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服务指北

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

原文  http://www.codesheep.cn/2018/12/10/eureka-server-https/
正文到此结束
Loading...