Eureka實戰-5【啟用https】

来源:https://www.cnblogs.com/idoljames/archive/2019/10/04/11623388.html
-Advertisement-
Play Games

上一篇主要說的是開啟http basic認證,從安全形度來講,基於base64編碼,容易被抓包後破解,在公網中很不安全,本文詳談如何在eureka server和eureka client中開啟https。 公共依賴pom文件 1、eureka server工程 1.1、eureka server工 ...


上一篇主要說的是開啟http basic認證,從安全形度來講,基於base64編碼,容易被抓包後破解,在公網中很不安全,本文詳談如何在eureka server和eureka client中開啟https。

 

公共依賴pom文件

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
</parent>

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>

 

1、eureka server工程

1.1、eureka server工程pom:

<!--加上文章頭部的公共依賴-->

<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

 

1.2、eureka server工程啟動類:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

 

1.3、生成證書

a、生成server工程的證書,使用命令行工具,執行下麵的指令:

keytool -genkeypair -alias server -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore server.p12 -validity 3650
輸入密鑰庫口令:
再次輸入新口令:
您的名字與姓氏是什麼?
  [Unknown]:  spring
您的組織單位名稱是什麼?
  [Unknown]:  spring
您的組織名稱是什麼?
  [Unknown]:  spring
您所在的城市或區功能變數名稱稱是什麼?
  [Unknown]:  spring
您所在的省/市/自治區名稱是什麼?
  [Unknown]:  spring
該單位的雙字母國家/地區代碼是什麼?
  [Unknown]:  spring
CN=spring, OU=spring, O=spring, L=spring, ST=spring, C=spring是否正確?
  [否]:  y

 

b、同樣的方式生成client工程證書,執行下麵的指令:

keytool -genkeypair -alias client -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore client.p12 -validity 3650
//。。。同樣需要設置密碼,回答上面的那些問題


c、分別導出2個p12證書,執行指令:

keytool -export -alias server -file server.crt --keystore server.p12
輸入密鑰庫口令: 這裡的密碼是生成server證書時設置的密碼
存儲在文件 <server.crt> 中的證書
keytool -export -alias client -file client.crt --keystore client.p12
輸入密鑰庫口令: 這裡的密碼是生成client證書時設置的密碼
存儲在文件 <client.crt> 中的證書

 

d、將server.crt文件導入client.p12證書中,使client端信任server的證書,執行如下指令

keytool -import -alias server -file server.crt -keystore client.p12
輸入密鑰庫口令: 這裡的密碼是生成client證書密碼,密碼錯誤會提示如下信息:
【密碼錯誤時提示的信息:keytool 錯誤: java.io.IOException: keystore password was incorrect】

所有者: CN=spring, OU=spring, O=spring, L=spring, ST=spring, C=spring 發佈者: CN=spring, OU=spring, O=spring, L=spring, ST=spring, C=spring 序列號: 2b87a269 有效期為 Fri Oct 04 20:11:07 CST 2019 至 Mon Oct 01 20:11:07 CST 2029 證書指紋: MD5: EF:A3:6B:32:DE:8F:E4:34:46:E6:0D:48:B9:8F:B8:7E SHA1: AE:42:78:14:D8:6B:B2:E9:46:F4:76:E8:D9:D0:51:E0:3A:E6:C9:2E SHA256: 54:6D:93:7E:B3:D3:C4:49:87:84:9D:46:66:B1:B8:1B:95:5B:DC:05:9A:8A:A4:DF:43:E4:A7:A7:4A:81:F7:B0 簽名演算法名稱: SHA256withRSA 主體公共密鑰演算法: 2048 位 RSA 密鑰 版本: 3 擴展: #1: ObjectId: 2.5.29.14 Criticality=false SubjectKeyIdentifier [ KeyIdentifier [ 0000: 6C 1A E4 01 EB 84 0B C2 90 97 81 3D DB 0D C3 F1 l..........=.... 0010: 4A FB 2A F4 J.*. ] ] 是否信任此證書? [否]: y 證書已添加到密鑰庫中

 

e、將client.crt文件導入server.p12文件,使server服務信任client的證書,執行如下指令:

keytool -import -alias client -file client.crt -keystore server.p12
輸入密鑰庫口令: 這裡的密碼是生成server證書密碼
所有者: CN=cloud, OU=cloud, O=cloud, L=cloud, ST=cloud, C=cloud
發佈者: CN=cloud, OU=cloud, O=cloud, L=cloud, ST=cloud, C=cloud
序列號: 6ea2a01
有效期為 Fri Oct 04 20:27:38 CST 2019 至 Mon Oct 01 20:27:38 CST 2029
證書指紋:
     MD5:  E9:22:2C:8D:C4:08:27:AD:02:75:93:31:C2:17:35:8E
     SHA1: BB:B0:9A:3A:98:43:5E:02:FC:8A:BC:85:33:DD:82:4A:4E:DF:3A:5C
     SHA256: 20:96:61:27:D1:CA:55:E5:B6:0E:41:CA:BC:84:F8:8F:1F:D8:25:87:10:50:90:E3:BC:12:39:35:74:16:4A:B3
簽名演算法名稱: SHA256withRSA
主體公共密鑰演算法: 2048 位 RSA 密鑰
版本: 3

擴展:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 91 E6 46 EF 4C 9E 88 B1   2F 63 12 4B 39 53 9D 32  ..F.L.../c.K9S.2
0010: EF 0F 42 F9                                        ..B.
]
]

是否信任此證書? [否]:  y
證書已添加到密鑰庫中

 

1.4、將生成的server.p12、server.crt、client.crt三個文件放在eureka server工程resources目錄下。

 

1.5、添加eureka server工程相關resources配置文件

application-https.yml:

server:
  port: 8766
  ssl:
    enabled: true
    key-store: classpath:server.p12
    key-store-password: hello2019 #生成server證書時設置的密碼
    key-store-type: PKCS12
    key-alias: server
eureka:
  instance:
    hostname: localhost
    securePort: ${server.port}
    securePortEnabled: true
    nonSecurePortEnabled: false
    homePageUrl: https://${eureka.instance.hostname}:${server.port}/
    statusPageUrl: https://${eureka.instance.hostname}:${server.port}/
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: https://${eureka.instance.hostname}:${server.port}/eureka/
  server:
      waitTimeInMsWhenSyncEmpty: 0
      enableSelfPreservation: false

application.yml:

spring:
  profiles:
    active: https

 

1.6、啟動eureka server工程,執行指令:

mvn spring-boot:run -Dspring.profiles.active=https

訪問:https://localhost:8766

 

 由此看出確實使用的是https,不妨在試一下訪問:http://localhost:8766

 

 

2、eureka client工程

2.1、client工程pom文件:

<!--加上文章頭部的公共依賴-->

<dependencies> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.5</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

 

2.2、client工程啟動類:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}

 

2.2、client工程resources配置文件

先將client.crt、client.p12、server.crt三個文件放在client工程的resources目錄下。

application.yml:

spring:
  profiles:
    active: https

application-https.yml:

server:
  port: 8081

spring:
  application:
    name: client1

eureka:
  client:
    securePortEnabled: true
    ssl:
      key-store: client.p12
      key-store-password: hello2020
    serviceUrl:
      defaultZone: https://localhost:8766/eureka/

這裡沒有指定整個應用實例啟用https,僅僅是開啟訪問eureka server的https配置,自定義eureka.client.ssl.key-store和eureka.client.ssl.key-store-password兩個屬性,指定client訪問server的sslContext配置,需要在代碼里指定DiscoveryClient.DiscoveryClientOptionalArgs:

import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.shared.transport.jersey.EurekaJerseyClientImpl;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

/**
 * 指定client訪問server的sslContext配置
 */
@Profile({"https"})
@Configuration
public class EurekaHttpsClientConfig {

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

    @Value("${eureka.client.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-https-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;
    }
}

 

2.3、執行指令,啟動client工程:

 mvn spring-boot:run -Dspring.profiles.active=https

訪問https://localhost:8766/

 

 可以看到client工程已經成功註冊到了server服務上。

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 職責鏈模式(Chain of Responsibility): 在現實生活中,常常會出現這樣的事例:一個請求需要多個對象處理,但每個對象的處理條件或許可權不同。如公司員工報銷差旅費,可審批的領導有部分負責人、副總經理、總經理等,但每個領導能審批的金額是不同的,不同的金額需要找相應的領導審批,也就是說要 ...
  • # -*- coding: utf-8 -*-"""Spyder Editor This is a temporary script file.tensor flow 之線性回歸模式2019-oct-5""" import tensorflow as tfimport numpy as npimpo ...
  • 本文主要講解Spring Boot 整合Jwt 認證的示例,詳細內容,詳見文末源碼。 ...
  • time模塊 作用:列印日期,做時間轉換。 import timeimport datetime #示例一:sleep()print("start to sleep.....")time.sleep(5) #讓程式停止5秒print("wake up...") #示例二:時間戳print(time. ...
  • 高強度訓練第二十天總結:Mybatis面試題 什麼是Mybatis? 1. Mybatis 是一個半 ORM(對象關係映射)框架,它內部封裝了 JDBC,開發時 只需要關註 SQL 語句本身,不需要花費精力去處理載入驅動、創建連接、創建 statement 等繁雜的過程。程式員直接編寫原生態 sql ...
  • CAP定理與BASE理論 CAP定理 2000 年 7 月,加州大學伯克利分校的 Eric Brewer 教授在 ACM PODC 會議上提出 CAP 猜想。2年後,麻省理工學院的 Seth Gilbert 和 Nancy Lynch 從理論上證明瞭 CAP。之後,CAP 理論正式成為分散式計算領域 ...
  • 概述 優點 第一 ,它解決了複雜問題。它把可能會變得龐大的單體應用程式分解成一套服務。雖然功能數量不變,但是應用程式已經被分解成可管理的塊或者服務。每個服務都有一個明確定義邊界的方式,如遠程過程調用(RPC)驅動或消息驅動 API。微服務架構模式強制一定程度的模塊化,實際上,使用單體代碼來實現是極其 ...
  • [TOC] 題目 "P1080 國王游戲" 思路 貪心+高精度。按$a \times b$從小到大排序就可以了、 $Code$ cpp include define MAXN 1001 define rr register using namespace std; const int Big_B = ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...