Eureka Server啓用 https服務指北

New Mac Mini

文章共 591字,閱讀大約須要 2分鐘 !

概 述

在個人前文《Eureka Server 開啓Spring Security Basic認證》中已經給 Eureka Server 開啓了最基本的鑑權措施,本文則讓 HTTPS加持於 Eureka Server,讓安全措施來的更完全一點。spring

注: 本文首發於 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

導出的證書在此:app

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

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配置文件裏將上文中生成的證書配到項目中去:ui

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來進行使用,代碼以下:this

@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。spa


實驗驗證

  • 啓動 Eureka Server,因爲其開啓了 https訪問,所以瀏覽器以非 https方式訪問時就不通了

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

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

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

  • 啓動 Eureka Client後,因爲其已經加入了對 https的配置,所以能夠驗證經過而且註冊到 Eureka Server註冊中心:

服務已經註冊上來

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

後 記

因爲能力有限,如有錯誤或者不當之處,還請你們批評指正,一塊兒學習交流!
相關文章
相關標籤/搜索