有时候因为安全问题,需要把配置文件的中数据库用户名密码由明文改成密文,大多数其实是为了应付甲方而已。
1.pom.xml引入依赖
1
2
3
4
5
|
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version> 2.1 . 0 </version> </dependency> |
2.自己想一个秘钥,然后弄一个main方法来测试和生成加密串,下面例子把“password”当做秘钥,加密 xiaoming 字符串。同样可以把加密的打印出来,放到解密里面去验证一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
//给配置文件加密 public static void main(String[] args) { // 加密 BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); //自己设置的秘钥 textEncryptor.setPassword( "password" ); String userName = textEncryptor.encrypt( "xiaoming" ); System.out.println(userName); // 解密 BasicTextEncryptor textEncryptor2 = new BasicTextEncryptor(); textEncryptor2.setPassword( "password" ); String oldPassword = textEncryptor2.decrypt( "avU0Q/XfNMXcgOgowdcfLfB1FDdApc292pzeq8/uvrllChedBJvj4A==" ); System.out.println(oldPassword); System.out.println( "--------------------------" ); } |
3.springboot配置文件 application.properties中添加配置
1
2
3
4
5
|
jasypt.encryptor.password=password spring.datasource.driver- class -name=oracle.jdbc.OracleDriver spring.datasource.url=jdbc:oracle:thin: @192 .168. 100.123 : 7029 :base spring.datasource.username=ENC(c31B0jWJp3EGFwqSkrUzhY //4CY/sO) spring.datasource.password=ENC(+KUeW5dB03CxJYz9oVV2flbYW5xs1+) |
要先声明秘钥,然后把刚main方法中加密出来的字符串替换原来的,注意一定要用ENC()把字符串包住才行。
然后重启就完事,就是这么简单。
到此这篇关于Springboot数据库配置文件明文密码加密解密的文章就介绍到这了,更多相关Springboot数据库密码加密解密内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/lianzhang861/article/details/123555635