zoukankan      html  css  js  c++  java
  • 微服务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

  • 相关阅读:
    1055. [HAOI2008]玩具取名【区间DP】
    BZOJ2435:[NOI2011]道路修建 (差分)
    1084. [SCOI2005]最大子矩阵【网格DP】
    1060. [ZJOI2007]时态同步【树形DP】
    1050. [HAOI2006]旅行【并查集+枚举】
    2463. [中山市选2009]谁能赢呢?【博弈论】
    luogu P1195 口袋的天空
    luogu P1162 填涂颜色
    luogu P1223 排队接水
    luogu P1331 海战
  • 原文地址:https://www.cnblogs.com/linjiqin/p/10338588.html
Copyright © 2011-2022 走看看