SpringBoot项目mysql配置文件密码加密(jasypt)

起因:因为我个人微博想要公开源码,但数据库配置文件会暴露在外面,又不想生产跟开发环境建来回切换,所以想到了加密数据库密码,于是问了群里的一个朋友小XX(原谅我不识字)

经过:

简单粗暴,直接上代码:

1.需要引入依赖

        <!-- 数据库加密 -->
        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

2.然后再yml或者properties中加入:

jasypt.encryptor.password=nmyswls

这个是盐值

3.在测试类中

@RunWith(SpringRunner.class)
@SpringBootTest
public class test_jiami {

    @Autowired
    StringEncryptor stringEncryptor;

    @Test
    public void encryptPwd() {
        String result = stringEncryptor.encrypt("root");
        System.out.println(result);
    }

}

这里面的参数是你的密码,然后返回来的是加密后的密码:yyrqk9reuY5kScmS3D9mWQ==

4.将加密后的密码贴到配置文件中,如下:

spring.datasource.password=ENC(yyrqk9reuY5kScmS3D9mWQ==)

这样就ok了,但我感觉如果你一不小心将你的密码在测试代码中提交上去,岂不是白加密了吗,其实还有一种方法:

在你的maven仓库中找到:D:\repository\org\jasypt\jasypt\1.9.2,这个包就是加密的包了

cmd在这个包下执行如下命令,它会返回你加密后的密码:

java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=zhang algorithm=PBEWithMD5AndDES

其中:

input:是数据库的明文密码

password:是机密的盐

algorithm:是加密的方式(默认)

然后在配置文件中把盐加进去就ok了

jasypt.encryptor.password=nmyswls

结果:妈妈再也不用担心我的密码被盗了~

猜你喜欢

转载自www.cnblogs.com/zhangjianbing/p/9184083.html
今日推荐