资讯专栏INFORMATION COLUMN

SpringCloud(第 030 篇)配置服务端ClientServer对配置文件内容进行对称加解

beita / 411人阅读

摘要:第篇配置服务端对配置文件内容进行对称加解密一大致介绍前面我们也简单讲解了如何搭建配置服务端微服务,也搭建了配置客户端微服务,但是呢,我们存储在上面的内容为明文,在生产环境的话,也不利于传输,特别一些重要的信息容易被泄露所以此章节,我们讲解

SpringCloud(第 030 篇)配置服务端ClientServer对配置文件内容进行对称加解密

-

一、大致介绍
1、前面我们也简单讲解了如何搭建配置服务端微服务,也搭建了配置客户端微服务,但是呢,我们存储在Git上面的内容为明文,在生产环境的话,也不利于传输,特别一些重要的信息容易被泄露;
2、所以此章节,我们讲解一下如何对文件的内容进行加密、解密,有利于内容在网络中的安全传输;

3、这里还顺便列举下配置路径的规则:
/****************************************************************************************
 * 配置服务的路劲规则:
 *
 * /{application}/{profile}[/{label}]
 * /{application}-{profile}.yml
 * /{label}/{application}-{profile}.yml
 * /{application}-{profile}.properties
 * /{label}/{application}-{profile}.properties
 ****************************************************************************************/
二、实现步骤 2.1 添加 maven 引用包


    4.0.0

    springms-config-server-encrypt
    1.0-SNAPSHOT
    jar

    
        com.springms.cloud
        springms-spring-cloud
        1.0-SNAPSHOT
    

    
        
        
            org.springframework.cloud
            spring-cloud-config-server
        
    

2.2 添加应用配置文件(springms-config-server-encrypt/src/main/resources/application.yml)
server:
  port: 8255

spring:
  application:
    name: springms-config-server-encrypt
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/ylimhhmily/OpenSource_CustomCircleLineProgressBar
#          username:    # 自己设置,这里就不做演示了
#          password:    # 自己设置,这里就不做演示了



encrypt:
  key: hehui  # 给配置文件的内容进行加密用的
  
2.3 添加应用启动类(springms-config-server-encrypt/src/main/java/com/springms/cloud/MsConfigServerEncryptApplication.java)
package com.springms.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * 配置服务端ClientServer对配置文件内容进行对称加解密。
* * @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
 ****************************************************************************************/
 
/****************************************************************************************
 一、配置服务端ClientServer对配置文件内容进行对称加解密(设置配置服务端文件对称加解密):

 1、注解:EnableConfigServer
 2、编辑 application.yml 文件,注意填写 encrypt.key 属性字段值,该值的作用在于给配置文件的内容进行加密用的;

 3、启动 springms-config-server-encrypt 模块服务,启动1个端口;

 4、下载 Java 8 JCE 文件,下载下来后文件名为 jce_policy-8.zip,然后将解压后的文件直接覆盖到 jdk 中,将 Javajdk1.8.0_92jrelibsecurity 路径下的文件覆盖即可;

 5、打开windows命令窗口,执行命令:
    >curl.exe localhost:8255/encrypt -d foobar-prd
    91e9d2e319f18d32de4821a5e932c759f93e0007dc66e69e0b9587e595e0f241

    >curl.exe localhost:8255/encrypt -d foobar-test
    3bf7b3e4adf9228d9fc70ecc168a33ff5269bc6efc66eaac8dde5c8e655303a0

    将这两个值进行保存到配置文件,也就是我们的Git仓库中的配置文件;
 6、在浏览器输入地址 http://localhost:8255/foobar-default.yml 正常情况下会输出配置文件的内容(内容为:profile: profile-default);
 7、在浏览器输入地址 http://localhost:8255/foobar-dev.yml 正常情况下会输出配置文件的内容(内容为:profile: profile-dev);
 8、在浏览器输入地址 http://localhost:8255/foobar-prd.yml 正常情况下会输出配置文件的内容(内容为:profile: foobar-prd);
 9、在浏览器输入地址 http://localhost:8255/foobar-test.yml 正常情况下会输出配置文件的内容(内容为:profile: foobar-test);
 10、在浏览器输入地址 http://localhost:8255/foobar-test.properties 正常情况下会输出配置文件的内容(内容为:profile: foobar-test);

 总结一:一切都正常打印,说明 SpringCloud 的解密已经能正确的完成了;

 11、修改 application.yml 文件,将 encrypt.key 属性值随便改下,改成比如 encrypt.key: aaaaaaaaaaa
 12、停止并重启 springms-config-server-encrypt 模块服务,启动1个端口;

 13、在浏览器输入地址 http://localhost:8255/foobar-default.yml 正常情况下会输出配置文件的内容(内容为:profile: profile-default);
 14、在浏览器输入地址 http://localhost:8255/foobar-dev.yml 正常情况下会输出配置文件的内容(内容为:profile: profile-dev);
 15、在浏览器输入地址 http://localhost:8255/foobar-prd.yml 不能正常获取配置文件内容(内容为:invalid: profile:  profile: profile-default);
 16、在浏览器输入地址 http://localhost:8255/foobar-test.yml 不能正常获取配置文件内容(内容为:invalid: profile:  profile: profile-default);
 17、在浏览器输入地址 http://localhost:8255/foobar-test.properties 不能正常获取配置文件内容(内容为:invalid: profile:  profile: profile-default);

 总结二:由此可见 encrypt.key 经过赋值生成配置文件内容后,就不能轻易改变,一旦改变的话,那么原本正常的内容值将获取不到了;
 ****************************************************************************************/
四、下载地址

https://git.oschina.net/ylimhhmily/SpringCloudTutorial.git

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信沟通群二维码图片链接

欢迎关注,您的肯定是对我最大的支持!!!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67785.html

相关文章

  • SpringCloud 032 配置服务ClientServer配置文件内容进行RSA加

    SpringCloud(第 032 篇)配置服务端ClientServer对配置文件内容进行RSA加解密 - 一、大致介绍 1、上章节我们讲解了对称加密配置文件内容,本章节我们讲解下非对称RSA加密配置文件; 2、这里还顺便列举下配置路径的规则: /**************************************************************************...

    crossea 评论0 收藏0
  • SpringCloud 031 配置客户ConfigClient链接经过加解密的配置

    摘要:添加应用启动类配置客户端链接经过对称加解密的配置微服务专门为测试经过对称加解密的配置微服务微服务模块。 SpringCloud(第 031 篇)配置客户端ConfigClient链接经过对称加解密的配置微服务 - 一、大致介绍 1、Git服务端的文件内容进行了加密处理,那么是不是配置客户端拿到内容之后需要解密呢? 2、答案显然不是的,因为这样解密的话,先不说实现起来的难易程度,单从表面...

    DDreach 评论0 收藏0
  • SpringCloud 033 配置客户ConfigClient链接经过加解密的配置

    SpringCloud(第 033 篇)配置客户端ConfigClient链接经过对称加解密的配置微服务 - 一、大致介绍 1、在(第 031 篇)讲解了如何链接对称加密的配置服务端,而链接对称非对称加密的配置微服务也是同样的; 2、配置客户端不需要做什么加解密的配置,加解密的配置在服务端做就好了; 3、这里还顺便列举下配置路径的规则: /****************************...

    ARGUS 评论0 收藏0
  • SpringCloud 029 配置客户 ConfigClient 接入配置服务

    SpringCloud(第 029 篇)配置客户端 ConfigClient 接入配置服务端 - 一、大致介绍 1、有配置服务端,那么势必就会有与之对应的客户端,SpringCloud 文档中集成也非常简单; 2、但是这里有点需要注意,就是 bootstrap 配置文件,官方建议我们在bootstrap中放置不更改的属性,我们同样也需要在这里做一些简单不易于改变的配置; 3、这里还顺便列举下配置...

    YFan 评论0 收藏0
  • SpringCloud 036 )单点手动动态刷新ConfigClient配置

    摘要:添加应用启动类单点手动动态刷新配置。配置客户端服务想要实现自动刷新配置的话,一端是不要做任何处理,只需要在一端处理即可。 SpringCloud(第 036 篇)单点手动动态刷新ConfigClient配置 - 一、大致介绍 1、当ConfigServer启动后,假如我们新增配置内容的话,是不是要重新启动一下ConfigServer呢? 2、答案肯定是不需要重新启动的,因为 Sprin...

    wanglu1209 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<