jasypt对application.yml数据源配置文件进行加密处理

1.在pom.xml添加jasypt-spring-boot-starter包  如下:

<dependency>
      <groupId>com.github.ulisesbocchio</groupId>
      <artifactId>jasypt-spring-boot-starter</artifactId>
      <version>1.14</version>
</dependency>

2.写一个工具类

public class CryptorTest {

    private static PooledPBEStringEncryptor initConfig() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword("longjinJavaEncryptor");
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        //config.setIvGeneratorClassName("org.jasypt.salt.NoOpIVGenerator");
//        config.setSaltGeneratorClassName("org.jasypt.salt.NoOpIVGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }

    public static void main(String[] args) {
        PooledPBEStringEncryptor encryptor = initConfig();
        System.out.println(encryptor.encrypt("111111"));
        System.out.println(encryptor.encrypt("222222222"));

    }
}

3.运行以上的代码   然后复制控制台打印的加密密码到相应的配置的中

4.继续调用接口看能否调通   如下说明加密成功了

发布了99 篇原创文章 · 获赞 26 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_39643007/article/details/93376655