使用jasypt加密springboot配置中的敏感数据 - Go语言中文社区

使用jasypt加密springboot配置中的敏感数据


一、操作流程

  1. 在springboot项目中引入jasypt的staters

    <dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot-starter</artifactId>
        <version>2.1.1</version>
    </dependency>
    
  2. springboot配置文件中添加如下配置:

    # xxx为加密密钥
    jasypt.encryptor.password=xxxx
    
  3. 编写单元测试,完成敏感数据加密

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = JasyptApplication.class)
    public class JasyptApplicationTests {
        @Autowired
        private StringEncryptor encryptor;
    
        @Test
        public void contextLoads() {
        }
    
        /**
         * 数据加密
         */
        @Test
        public void testEncrypt(){
            String username = encryptor.encrypt("root");
            String password = encryptor.encrypt("root");
            String url = encryptor.encrypt("jdbc:mysql://localhost:3306/dtabase");
            String redis = encryptor.encrypt("localhost");
    
            System.out.println("username:" +username);
            System.out.println("password:" +password);
            System.out.println("url:" +url);
            System.out.println("redis:" +redis);
        }
    
        /**
         * 数据解密
         */
        @Test
        public void  testDecrypt(){
            String username = encryptor.decrypt("BFqR8ccKjXnxV1vVp7nOFg==");
            String password = encryptor.decrypt("X720Oc+o5bZ+eGN3tr81dA==");
            String url = encryptor.decrypt("iSVNDs1fnQL9DXJSK/7whm2O17IiG2l9ae90mjT");
            String redis = encryptor.decrypt("IGrDzDuenxK+HK1dA8PH56Kvde0o+qbt");
            
            System.out.println("username:" +username);
            System.out.println("password:" +password);
            System.out.println("url:" +url);
            System.out.println("redis:" +redis);
        }
    }
    
  4. 替换敏感数据(ENC() 为默认前缀、后缀,可替换 )
    加密后配置文件

  5. 密钥隐藏

    方式一:推荐使用

    1. 将密钥添加到环境变量(环境变量名与配置文件${}中的命名一致)

      • windows环境变量配置

        配置jasypt环境变量

      • linux环境变量配置(未测试)

        #1. 打开/etc/profile文件
        vim /etc/profile
        
        #2. 文件末尾插入
        export JASYPT_PASSWORD = G0CvDz7oJn6
        
        #3. 编译 
        source /etc/profile
        
    2. 修改springboot配置文件jasypt.encryptor.password属性

      jasypt.encryptor.password=${JASYPT_ENCRYPTOR_PASSWORD:}
      
    3. java -jar xxx.jar启动应用

    方式二:

    1. 删除springboot配置文件jasypt.encryptor.password属性

    2. 启动应用

      java -jar xxx.jar --jasypt.encryptor.password=xxx
      或
      java -Djasypt.encryptor.password=xxx -jar xxx.jar
      

二、应用启动备注

  1. 添加环境变量后且未配置jasypt.encryptor.password属性还可以通过如下方式启动应用

    1.1 命令行启动:

    java -jar xxx.jar --jasypt.encryptor.password=${ENCRYPTOR_PASSWORD}
    # 或
    java -jar -Djasypt.encryptor.password=${ENCRYPTOR_PASSWORD} xxx.jar
    

    1.2 idea启动:

    # 添加Environmental variables
    JASYPT_PASSWORD=xxx
    
  2. 未添加环境变量未配置jasypt.encryptor.password属性

    2.1 命令行启动:

    java -jar xxx.jar --jasypt.encryptor.password=xxx
    # 或
    java -Djasypt.encryptor.password=xxx -jar xxx.jar
    

    2.2 idea启动:

    # 方式一:添加VM options
    -Djasypt.encryptor.password=password
    # 方式二:添加Program arguments
    --jasypt.encryptor.password=password
    

三、参考文档:

  1. 官方Git地址:https://github.com/ulisesbocchio/jasypt-spring-boot
  2. 加密原理解析:https://blog.csdn.net/u013905744/article/details/86508236
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/Ajlzhu/article/details/91866782
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-07 22:29:26
  • 阅读 ( 833 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢