一、前面咱们也简单讲解了如何搭建配置服务端微服务,也搭建了配置客户端微服务,可是呢,咱们存储在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