Spring Boot 集成 jasypt-spring-boot - 实现配置文件加密

当前版本3.0.3 ,3.0之前版本测试没有成功,目前整理3.0.3可以

  • 添加pom依赖
<!-- jasypt -->
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>
  • yml中添加配置文件
jasypt:
  encryptor:
    password: pswenc
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

PS:可以看到配置中特意配置了加密算法,原因是官方在 3.0.0 以后更改了加密算法,所以假如你不设置的话,使用网上的方法加密出来的密码启动就会报错,如图: (也有可能是生成密文的工具和当前版本默认的加密工具不同也会导致这个问题)

工具类

package com.primeton.bps.Jasypt;

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

/**
 * @author : xifanzhou
 * @Description :
 */
public class JasyptUtil {


    /**
     * Jasypt生成加密结果
     * @param password 配置文件中设定的加密盐值
     * @param value 加密值
     * @return
     */
    public static String encyptPwd(String password,String value){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(password));
        String result = encryptor.encrypt(value);
        return result;
    }

    /**
     * 解密
     * @param password 配置文件中设定的加密盐值
     * @param value 解密密文
     * @return
     */
    public static String decyptPwd(String password,String value){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(password));
        String result = encryptor.decrypt(value);
        return result;
    }

    public static SimpleStringPBEConfig cryptor(String password){
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        return config;
    }


    public static void main(String[] args) {
        // 加密
        String encPwd = encyptPwd("pswenc", "root");
        // 解密
        String decPwd = decyptPwd("pswenc", encPwd);
        System.out.println(encPwd);
        System.out.println(decPwd);
    }
}

 将生成的密文替换原来配置的明文中去

 启动项目则会自动解密为明文来加载

  •   如何安放你的密钥

密钥是非常重要的信息,放在什么地方,决定着你的密文是否真的安全。可以有以下几类方式:

(1)放在application.properties

这样能获得配置文件的人就能知道密钥,不够安全。但它是一种方便简单的方式。存在密文和密钥放在同一个配置文件的风险。

(2)JVM参数

在启动Java程序时加参数:-Djasypt.encryptor.password=pswenc,这样就不会把密钥放在代码中去了。

(3)服务器的环境变量

把密钥放在linux系统的环境变量中去,只有能拿到服务器访问权限的人,才有可能知道密钥在哪。例如:

# profile文件末尾中添加
export JASYPT_PASSWORD=pswenc

# 生效 
source /etc/profile

# 运行java程序时
java -jar -Djasypt.encryptor.password=${JASYPT_PASSWORD} xxx.jar

(4)使用自定义的Encryptor来存放

以上我们都使用了官方提供的Encryptor,其实我们是可以自定义的,如下:

@Bean("jasyptStringEncryptor")
public StringEncryptor stringEncryptor() {
  PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
  SimpleStringPBEConfig config = new SimpleStringPBEConfig();
  config.setPassword("password");
  config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
  config.setKeyObtentionIterations("1000");
  config.setPoolSize("1");
  config.setProviderName("SunJCE");
  config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
  config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
  config.setStringOutputType("base64");
  encryptor.setConfig(config);
  return encryptor;
}

把密钥写在代码里,只有能获得jar包并反编译的人,才能获得密文。

如果我们把密钥的一部分写在代码里,另一部分通过外部方式来配置,这样就会更加安全。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值