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

本文介绍了如何在SpringBoot项目中使用jasypt加密敏感数据,包括引入依赖、配置加密前缀、编写单元测试加密、设置环境变量存储密钥,以及在不同环境下启动应用的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、操作流程

  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.youkuaiyun.com/u013905744/article/details/86508236
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值