公司專案越來越大了,公司對資料的重要性越來越重視了,要求我們開發組出一個方案,現在開發大部分密碼都是明文的,一不小心泄露了,所有的重要資訊就公眾于世了,這是一個很大的安全隱患,jasypt就能很好的隱藏明文密碼,
1.基于jdk8、jdk11參考jasypt jar包的依賴
注:jar包依賴的 jasypt-1.9.2.jar位置:E:\.m2\repository\org\jasypt\jasypt\1.9.2\ jasypt-1.9.2.jar(這是本人地址,生成加密的命令時用到),根據自己的maven設定的地址找到自己的jar包位置,
<!--jasypt資料庫加密-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
2.打開DOS界面,輸入一下命令生成加密后的密碼
C:\Users\dongs>java -cp E:\.m2\repository\org\jasypt\jasypt\1.9.2\jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=***@2021 algorithm=PBEWithMD5AndDES
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 11.0.9+7-LTS
----ARGUMENTS-------------------
input: postgres
password: *****@2021
algorithm: PBEWithMD5AndDES
----OUTPUT----------------------
Y/Y6********+VhP6Xz4b
注釋:input:寫入的是資料庫連接密碼
password:相當于密碼鹽(自己隨意定義)
algorithm:演算法(一般不改動)
output:生成加密后的密碼
3.yml檔案配置
將上述自己定義的密碼鹽配置在yml檔案
jasypt:
encryptor:
password: dhst@2021
將明文的資料庫密碼改為加密后的密碼,password:ENC(加密的密碼),記住:加密密碼一定放在ENC()內,以區分是加密的還是無加密的,
spring:
datasource:
name: master
url: jdbc:postgresql://*.*.*.*:5432/****?stringtype=unspecified
username: postgres
password: ENC(Y/Y6E********VhP6Xz4b)
4.在啟動類中,添加注解@EnableEncryptableProperties,使其配置生效
@Slf4j
@EnableScheduling
@EnableAsync
@MapperScan("com.ahdhst.lzz.assessment.mapper")
@EnableEncryptableProperties
public class MainApplication extends CommonApp {
public static void main(String[] args) {
/** 配置加解密跟秘鑰,與組態檔的密文分開放 */
System.setProperty("jasypt.encryptor.password", DHConstants.JASYPT_ENCRYPTOR_PASSWORD);
SpringApplication.run(MainApplication.class, args);
}
}
注:
System.setProperty("jasypt.encryptor.password", DHConstants.JASYPT_ENCRYPTOR_PASSWORD);這種寫法是取代將密碼鹽配置在yml,在啟動類寫入了這行代碼,第二步的yml配置密碼鹽可以不用配置,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/254500.html
標籤:其他
上一篇:寶塔面板下,如何通過公網IP+埠號訪問自己不同的網站
下一篇:wifi連接問題debug
