软件编程
位置:首页>> 软件编程>> java编程>> 关于Springboot数据库配置文件明文密码加密解密的问题

关于Springboot数据库配置文件明文密码加密解密的问题

作者:豆趣编程  发布时间:2023-11-25 03:29:46 

标签:Springboot,数据库,密码,加密,解密

有时候因为安全问题,需要把配置文件的中数据库用户名密码由明文改成密文,大多数其实是为了应付甲方而已。

1.pom.xml引入依赖


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

2.自己想一个秘钥,然后弄一个main方法来测试和生成加密串,下面例子把&ldquo;password&rdquo;当做秘钥,加密 xiaoming 字符串。同样可以把加密的打印出来,放到解密里面去验证一下

//给配置文件加密
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中添加配置

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()把字符串包住才行。

然后重启就完事,就是这么简单。

来源:https://blog.csdn.net/lianzhang861/article/details/123555635

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com