微服務SpringCloud—Config Server對稱加密


配置內容的加解密
在Git倉庫中明文存儲配置屬性的。很多場景下,對於某些敏感的配置內容(例如數據庫賬號、密碼等),應當加密存儲。

Config對稱加解密
1、安裝JCE
默認情況下我們的JRE自帶了JCE,但是默認是一個有限長度的版本,需要到oracle官網下載一個不限長度的JCE。

JCE下載地址
https://www.oracle.com/technetwork/java/javase/downloads/jce-all-download-5170447.html

下載JCE並解壓(eg:jce_policy-8.zip),按照其中的README.txt的說明安裝。

JCE的安裝非常簡單,其實就是將JDK/jre/lib/security目錄中的兩個jar文件(local_policy.jar、US_export_policy.jar)替換為壓縮包中的jar文件。

2、在config server服務的bootstrap.yml文件中配置對稱密匙

#博客:https://blog.csdn.net/u014296316/article/details/80881974
#http://localhost:6063/encrypt/status 驗證加密解密功能是否正常
#http://localhost:6063/encrypt 只允許post請求
#http://localhost:6063/decrypt 只允許post請求
encrypt: key: Lynch

 

3、訪問 http://localhost:6063/encrypt/status 驗證加密解密功能是否正常

4、訪問/encrypt和/decrypt進行加密解密
http://localhost:6063/encrypt 只允許post請求

http://localhost:6063/decrypt 只允許post請求

5、配置文件中使用{cipher}開頭標識加密數據

6、在Config Client服務獲取加密數據

import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * 這邊的@RefreshScope注解不能少,否則即使調用/refresh,配置也不會刷新 */ @RestController @RefreshScope public class ConfigClientController { @Value("${env}") private String env; @Value("${password}") private String password; @Value("${username}") private String username; @GetMapping("/config/profile") public String hello() { return this.env+","+this.password+","+this.username; } }

 

 

http://localhost:6062/config/profile


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM