十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
出于安全考慮,明文的數(shù)據(jù)庫賬號(hào)密碼存在安全隱患,需要對(duì)其進(jìn)行加密
這里采用jasypt進(jìn)行處理
引入jasypt依賴
com.github.ulisesbocchio jasypt-spring-boot-starter3.0.3
配置文件中添加password信息
jasypt:
encryptor:
password: silen
編寫測(cè)試代碼,得到加密結(jié)果
import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class JasyptApplicationTests {
@Autowired
private StringEncryptor encryptor;
@Test
public void testEncrypt() {
System.out.println(encryptor.encrypt("root")); // iozDcujQo575YtsZ1giHl/zRZM5Si6arM8EA4wMOogG2x8L4xZIQsf7dEUSG8b9A
System.out.println("解密:"+encryptor.decrypt("iozDcujQo575YtsZ1giHl/zRZM5Si6arM8EA4wMOogG2x8L4xZIQsf7dEUSG8b9A"));
}
}
將對(duì)應(yīng)的數(shù)據(jù)填入配置文件中
username: ENC(iozDcujQo575YtsZ1giHl/zRZM5Si6arM8EA4wMOogG2x8L4xZIQsf7dEUSG8b9A)
password: ENC(iozDcujQo575YtsZ1giHl/zRZM5Si6arM8EA4wMOogG2x8L4xZIQsf7dEUSG8b9A)
不想使用默認(rèn)的ENC(),可自定義設(shè)置,配置文件添加代碼
jasypt:
encryptor:
password: silen
property:
prefix: silen(
suffix: )
#對(duì)應(yīng)加密位置則為
username: silen(iozDcujQo575YtsZ1giHl/zRZM5Si6arM8EA4wMOogG2x8L4xZIQsf7dEUSG8b9A)
password: silen(iozDcujQo575YtsZ1giHl/zRZM5Si6arM8EA4wMOogG2x8L4xZIQsf7dEUSG8b9A)
password放在配置文件中依然存在被破解的風(fēng)險(xiǎn),可以考慮在執(zhí)行jar文件的時(shí)候?qū)⒃搮?shù)添加進(jìn)去,執(zhí)行命令如下
java -jar admin.jar --jasypt.encryptor.password=silen
若在執(zhí)行3的測(cè)試代碼時(shí)出現(xiàn)報(bào)錯(cuò)信息
org.jasypt.exceptions.EncryptionOperationNotPossibleException: Encryption raised an exception. A possible cause is you are using strong encryption algorithms and you have not installed the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction
解決方案:
去到官網(wǎng)下載了兩個(gè)和安全相關(guān)的包之后問題解決。
下載地址:https://www.oracle.com/java/technologies/javase-jce8-downloads.html
下載之后放置到JAVA_HOME\jdk1.8.0_72\jre\lib\security目錄底下,重新運(yùn)行加解密代碼問題解決。
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧