springboot整合jasypt对yml配置文件密码加密

发布时间:2024年01月11日

1.引入依赖

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

		<dependency>
			<groupId>org.jasypt</groupId>
			<artifactId>jasypt</artifactId>
			<version>1.9.2</version>
		</dependency>

2.启动类增加@EnableEncryptableProperties

@EnableEncryptableProperties

3.生成加密密码

public class Test {
    public static void main(String[] args) {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        //加密的算法,默认
        config.setAlgorithm("PBEWithMD5AndDES");
        //加密的盐
        config.setPassword("password");
        standardPBEStringEncryptor.setConfig(config);
        //需要加密的密码
        String password = "root";
        String encrypt = standardPBEStringEncryptor.encrypt(password);
        System.out.println(encrypt);

        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword("password");
        System.out.println(encryptor.decrypt(encrypt));
    }
}

4.配置文件密码加密

    password: ENC(Kn7cQm1DtLutH/27dTPi3D(Q/ILQbOKK)

加密的密码放在括号里面

5.配置秘钥

#jasypt加密的密匙
jasypt:
  encryptor:
    password: root

6.放在配置文件容易泄露,可以配置启动命令

java -jar -Djasypt.encryptor.password=root XXX-xxxx.jar
文章来源:https://blog.csdn.net/weixin_45810161/article/details/135519786
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。