1. 程式人生 > >springcloud(三):配置中心(Config)對稱加密

springcloud(三):配置中心(Config)對稱加密

概述

從配置獲取的配置預設是明文的,有些像資料來源這樣的配置需要加密的話,需要對配置中心進行加密處理。

下面使用對稱性加密來加密配置,需要配置一個金鑰,當然也可以使用RSA非對稱性加密,但對稱加密比較方便也夠用了,這裡就以對稱加密來配置即可。

如何引入config encrypt server

 <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
 <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>

如何執行一個config Server

@EnableConfigServer
@SpringBootApplication
public class ConfiglocalServerApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(ConfiglocalServerApplication.class).run(args);
    }
}
spring:
 profiles:
     active: native   //讀取本地配置檔案
 cloud:
    config:
      server:
        native:
          search-locations: classpath:/configPro  # 自動抓取src/main/resources/configPro/資料夾下的配置

安裝JCE

JDK下的JCR預設是有長度限制的,需要替換沒有長度限制的JCE版本。

把下載包裡面的兩個jar檔案複製替換到JAVA_HOME/jre/lib/security目錄下。

新增加密KEY

配置中心配置檔案中加入加密金鑰。 encrypt: key: 0e010e17-2529-4581-b907-c8edcfd6be09

檢視加密功能狀態

功能正常會顯示OK {"status":"OK"}

加密解密

對 client字串加密
curl http://localhost:8888/encrypt -d client
對 client字串解密
curl http:// localhost:8888/decrypt -d  6c5f244255e335947b5383e51344150c69aba8c0bce0c0d86fb29716bb4c4601

配置檔案

config:
  client:  '{cipher}6c5f244255e335947b5383e51344150c69aba8c0bce0c0d86fb29716bb4c4601'

需要加密的內容以{cipher}開頭,並注意要使節單引號包起來,不然報錯。

讀取配置

這樣客戶端讀取出來的配置是自動解密了的,如果要關閉自動解密功能通過客戶端自己來解密,同時也要保留加解密的端點可以通過關閉以下配置即可。