一、前面咱們也簡單講解了如何搭建配置服務端微服務,也搭建了配置客戶端微服務,可是呢,咱們存儲在Git上面的內容爲明文,在生產環境的話,也不利於傳輸,特別一些重要的信息容易被泄露; 二、因此此章節,咱們講解一下如何對文件的內容進行加密、解密,有利於內容在網絡中的安全傳輸; 三、這裏還順便列舉下配置路徑的規則: /**************************************************************************************** * 配置服務的路勁規則: * * /{application}/{profile}[/{label}] * /{application}-{profile}.yml * /{label}/{application}-{profile}.yml * /{application}-{profile}.properties * /{label}/{application}-{profile}.properties ****************************************************************************************/
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>springms-config-server-encrypt</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>com.springms.cloud</groupId> <artifactId>springms-spring-cloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <dependencies> <!-- 服務端配置模塊 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> </project>
server: port: 8255 spring: application: name: springms-config-server-encrypt cloud: config: server: git: uri: https://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar # username: # 本身設置,這裏就不作演示了 # password: # 本身設置,這裏就不作演示了 encrypt: key: hehui # 給配置文件的內容進行加密用的
package com.springms.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; /** * 配置服務端ClientServer對配置文件內容進行對稱加解密。<br/> * * @author hmilyylimh * * @version 0.0.1 * * @date 17/10/18 * */ @SpringBootApplication @EnableConfigServer public class MsConfigServerEncryptApplication { public static void main(String[] args) { SpringApplication.run(MsConfigServerEncryptApplication.class, args); System.out.println("【【【【【【 ConfigServerEncrypt微服務 】】】】】】已啓動."); } }
/**************************************************************************************** application.yml 涉及到的連接文件內容展現以下: http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/application.yml profile: profile-default http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-dev.yml profile: profile-dev http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-prd.yml profile: '{cipher}91e9d2e319f18d32de4821a5e932c759f93e0007dc66e69e0b9587e595e0f241' http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-test.properties profile: {cipher}3bf7b3e4adf9228d9fc70ecc168a33ff5269bc6efc66eaac8dde5c8e655303a0 ****************************************************************************************/ /**************************************************************************************** 1、配置服務端ClientServer對配置文件內容進行對稱加解密(設置配置服務端文件對稱加解密): 一、註解:EnableConfigServer 二、編輯 application.yml 文件,注意填寫 encrypt.key 屬性字段值,該值的做用在於給配置文件的內容進行加密用的; 三、啓動 springms-config-server-encrypt 模塊服務,啓動1個端口; 四、下載 Java 8 JCE 文件,下載下來後文件名爲 jce_policy-8.zip,而後將解壓後的文件直接覆蓋到 jdk 中,將 Java\jdk1.8.0_92\jre\lib\security 路徑下的文件覆蓋便可; 五、打開windows命令窗口,執行命令: >curl.exe localhost:8255/encrypt -d foobar-prd 91e9d2e319f18d32de4821a5e932c759f93e0007dc66e69e0b9587e595e0f241 >curl.exe localhost:8255/encrypt -d foobar-test 3bf7b3e4adf9228d9fc70ecc168a33ff5269bc6efc66eaac8dde5c8e655303a0 將這兩個值進行保存到配置文件,也就是咱們的Git倉庫中的配置文件; 六、在瀏覽器輸入地址 http://localhost:8255/foobar-default.yml 正常狀況下會輸出配置文件的內容(內容爲:profile: profile-default); 七、在瀏覽器輸入地址 http://localhost:8255/foobar-dev.yml 正常狀況下會輸出配置文件的內容(內容爲:profile: profile-dev); 八、在瀏覽器輸入地址 http://localhost:8255/foobar-prd.yml 正常狀況下會輸出配置文件的內容(內容爲:profile: foobar-prd); 九、在瀏覽器輸入地址 http://localhost:8255/foobar-test.yml 正常狀況下會輸出配置文件的內容(內容爲:profile: foobar-test); 十、在瀏覽器輸入地址 http://localhost:8255/foobar-test.properties 正常狀況下會輸出配置文件的內容(內容爲:profile: foobar-test); 總結一:一切都正常打印,說明 SpringCloud 的解密已經能正確的完成了; 十一、修改 application.yml 文件,將 encrypt.key 屬性值隨便改下,改爲好比 encrypt.key: aaaaaaaaaaa 十二、中止並重啓 springms-config-server-encrypt 模塊服務,啓動1個端口; 1三、在瀏覽器輸入地址 http://localhost:8255/foobar-default.yml 正常狀況下會輸出配置文件的內容(內容爲:profile: profile-default); 1四、在瀏覽器輸入地址 http://localhost:8255/foobar-dev.yml 正常狀況下會輸出配置文件的內容(內容爲:profile: profile-dev); 1五、在瀏覽器輸入地址 http://localhost:8255/foobar-prd.yml 不能正常獲取配置文件內容(內容爲:invalid: profile: <n/a> profile: profile-default); 1六、在瀏覽器輸入地址 http://localhost:8255/foobar-test.yml 不能正常獲取配置文件內容(內容爲:invalid: profile: <n/a> profile: profile-default); 1七、在瀏覽器輸入地址 http://localhost:8255/foobar-test.properties 不能正常獲取配置文件內容(內容爲:invalid: profile: <n/a> profile: profile-default); 總結二:因而可知 encrypt.key 通過賦值生成配置文件內容後,就不能輕易改變,一旦改變的話,那麼本來正常的內容值將獲取不到了; ****************************************************************************************/
https://gitee.com/ylimhhmily/SpringCloudTutorial.gitjava
SpringCloudTutorial交流QQ羣: 235322432git
SpringCloudTutorial交流微信羣: 微信溝通羣二維碼圖片連接spring
歡迎關注,您的確定是對我最大的支持!!!apache