首页

使用springboot-jasypt实现数据库密码密文配置超简单maven项目下载

标签:jasypt示例,ulisesbocchio,StringEncryptor,encrypt,decrypt,数据库密码,敏感密码转码,ENC,DEC     发布时间:2022-04-14   
  • 云盘下载:
  • [提取码:vm4u]
  • 本地下载:
       ( 需积分:8  )

一、项目背景

通过springboot项目使用jasypt对数据库密码进行加密配置,实现代码或配置不出现敏感字段,从而保证密码等字段不被泄露出去。

二、代码说明

1、如下配置对于数据库密码password配置为ENC(密码加密结果,可以通过jasypt工具进行加密)

server:@b@  port: 8000@b@spring:@b@  datasource:@b@    driver-class-name: com.mysql.cj.jdbc.Driver@b@    url: jdbc:mysql://localhost:3306/mydb?autoReconnect=true&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8@b@    username: ENC(48cjipzsicwvVijCLzJAAQ==)@b@    password: ENC(PW3VWsj4fgPkiOojGJA9Cw==)@b@    abc: abc123@b@  jpa:@b@    hibernate:@b@      ddl-auto: update@b@    show-sql: true@b@  jackson:@b@    default-property-inclusion: non_null@b@    date-format: yyyy-MM-dd HH:mm:ss@b@    serialization:@b@      write-dates-as-timestamps: false@b@    time-zone: GMT+8@b@jasypt:@b@  encryptor:@b@    password: 123456

2、为了保证jasypt密码不泄露,可以上面yml配置倒数三行关于jasypt密码123456配置删除,改为vm参数注入

 #VM options @b@  -Djasypt.encryptor.password=123456

3、点击http://localhost:8000/getConfig接口获取jasypt明文值

@GetMapping("/getConfig")@b@public String readConfig(){@b@    System.out.println("dbUrl:" + dbUrl);@b@    System.out.println("dbUserName:" + dbUserName);@b@    System.out.println("dbPassword:" + dbPassword);@b@    System.out.println("abc:" + abc);@b@    return "success";@b@}
dbUrl:jdbc:mysql://localhost:3306/mydb?autoReconnect=true&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8@b@dbUserName:root@b@dbPassword:123456@b@abc:abc123

4、点击http://localhost:8000/encryptortTest接口,获取加解密结果

@GetMapping("/encryptortTest")@b@public String getEnc() {@b@    String urlEnc = encryptor.encrypt("jdbc:mysql://localhost:3306/mydb?autoReconnect=true&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8");@b@    String nameEnc = encryptor.encrypt("root");@b@    String passwordEnc = encryptor.encrypt("root");@b@    System.out.println("jasypt加密 dbUrlEnc: " + urlEnc);@b@    System.out.println("jasypt加密 dbUserNameEnc: " + nameEnc);@b@    System.out.println("jasypt加密 dbPasswordEnc: " + passwordEnc);@b@@b@    System.out.println("jasypt解密 dbUrlDec: " + encryptor.decrypt(urlEnc));@b@    System.out.println("jasypt解密 dbUserNameDec: " + encryptor.decrypt(nameEnc));@b@    System.out.println("jasypt解密 dbPasswordDec: " + encryptor.decrypt(passwordEnc));@b@    return "success";@b@}
jasypt加密 dbUrlEnc: 16RMktwuhnLfxIMI9od7IGP7Zht+DU3mK0/Zpopnsd0BnLIwVkZYdbiGMO12+KAS91T+aH4HBdtG5KaCQOpzX4bzAAN/O+PkpsvIEUk2RWNukDdmVRhJFSGpeUq9+UQXvghBS8O0lh3NkTqvnEgz7jqq34IlmMlR6FHi79WKG9M=@b@jasypt加密 dbUserNameEnc: 2rus+E+SFQscsanJcZ2r5Q==@b@jasypt加密 dbPasswordEnc: ahPmbXtx1DOa+D9KvhbxSA==@b@jasypt解密 dbUrlDec: jdbc:mysql://localhost:3306/mydb?autoReconnect=true&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8@b@jasypt解密 dbUserNameDec: root@b@jasypt解密 dbPasswordDec: root
  • <<相关内容>>
<<热门下载>>