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

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方式訪問時就不通了

非 https的方式是無法訪問註冊中心的

瀏覽器必須以 https方式訪問註冊中心方可:

以 https方式訪問註冊中心方可

  • 啓動 Eureka Client後,由於其已經加入了對 https的配置,因此可以驗證通過並且註冊到 Eureka Server註冊中心:

服務已經註冊上來

如此一番實踐下來,微服務註冊中心的安全性就更進了一步。


後 記

由於能力有限,若有錯誤或者不當之處,還請大家批評指正,一起學習交流!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章