1.使用環(huán)境
springboot:2.1.4.RELEASE
JDK:8
jasypt-spring-boot-starter:3.0.2
Jasypt默認(rèn)算法為PBEWithMD5AndDES,該算法需要一個(gè)加密密鑰,可以在應(yīng)用啟動(dòng)時(shí)指定(環(huán)境變量)。也可以直接寫(xiě)入配置文件
2.引入依賴
!-- 配置文件加密 -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
3.加密方式
3.1 application.properties配置文件版
server.port=8080
### mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.xx.xx
### xxl-job, datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=ENC(jGfd15IFYKMjMW5bUYp06BQ8OCdHTXi3zbD4j9tw6U3PPvvA5GS8zn10kgVmgxXY)
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#加密鹽值,獲取加密后的值后刪除,在vmOptions配置(如下圖)
jasypt.encryptor.password=123adf
@SpringBootTest
@RunWith(SpringRunner.class)
public class D2ApplicationTests {
@Resource
private StringEncryptor jasyptStringEncryptor;
@Test
public void contextLoads() {
String pwd = jasyptStringEncryptor.encrypt("root");
System.out.println("pwd:" + pwd);
}
}
加密后,可刪除jasypt.encryptor.password配置;發(fā)版時(shí)可在命令行中配置
3.2 函數(shù)版
// 創(chuàng)建加密對(duì)象,默認(rèn) PBEWithMD5AndDES
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
// 加密所需的密鑰
textEncryptor.setPassword("password");
// 加密后的數(shù)據(jù)(數(shù)據(jù)庫(kù)的用戶名或密碼)
String encData = textEncryptor.encrypt("Password@1");
// 解密后的數(shù)據(jù)(原數(shù)據(jù))
String decData = textEncryptor.decrypt(encData);
System.out.println("encData: " + encData);
System.out.println("decData: " + decData);
4.所遇問(wèn)題
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 Policy Files in this Java Virtual Machine
解決方案:java密碼擴(kuò)展無(wú)限制權(quán)限策略文件安裝–jce_policy安裝
文件下載地址:
下面為下載鏈接
JDK 1.7–jce安裝地址:JDK1.7 -jce地址
JDK 1.8–jce安裝地址:JDK 1.8–jce安裝地址
相關(guān)文件來(lái)自于博客:https://www.cnblogs.com/zgngg/p/13859299.html) 感謝博主:瘋狂的⑨醬 的無(wú)私奉獻(xiàn)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-610088.html
jar配置路徑:
JDK:將兩個(gè)jar文件放到%JDK_HOME%\jre\lib\security下
JRE:將兩個(gè)jar文件放到%JRE_HOME%\lib\security下文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-610088.html
到了這里,關(guān)于SpringBoot+jasypt-spring-boot-starter實(shí)現(xiàn)配置文件明文加密的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!