SpringBoot項目整合jasypt實現(xiàn)過程詳解
依賴引入pom.xml
<!-- jasypt核心依賴 --> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.1</version> <!-- jasypt2.1.1與spring-boot2.2.6的兼容性是最好的,避免踩坑,淚呀 --> </dependency> <!-- jasypt-maven插件,不影響基本功能 --> <plugin> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-maven-plugin</artifactId> <version>3.0.3</version> </plugin>
配置參數(shù)application.properties
jasypt.encryptor.password=lE1rl5K$
crypt.user-name=ENC(qvh/QiJYOHNNiJWqhek5Xw==)
crypt.password=ENC(oriTNJoCp5lQ0Tyj5JJmzQ==)
kkk=DEC(123456)
測試代碼
package com.yang.ftpdemo.controller; import lombok.Data; import org.jasypt.encryption.StringEncryptor; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @RequestMapping("/crypt") public class CryptController { @Resource private StringEncryptor encrypt; @Resource private CryptConfig cryptConfig; @GetMapping("/encrypt") public CryptConfig encrypt() { String username = encrypt.encrypt("root"); String password = encrypt.encrypt("root123"); CryptConfig crypt = new CryptConfig(); crypt.setPassword(password); crypt.setUserName(username); return crypt; } @GetMapping("/decrypt") public CryptConfig decrypt() { CryptConfig crypt = new CryptConfig(); BeanUtils.copyProperties(this.cryptConfig, crypt); return crypt; } } @Data @Configuration @ConfigurationProperties(prefix = "crypt") class CryptConfig { private String userName; private String password; }
測試
瀏覽器訪問【http://localhost:8080/crypt/encrypt】得到加密結果,并且每次請求結果不一樣:
{ "userName":"XsWOwhZIag8XBh3DFl4sqA==", "password":"3kD2/u+xnM1i5mO2cVMWKw==" }
瀏覽器訪問【http://localhost:8080/crypt/decrypt】得到解密結果,每次請求結果一樣:
{ "userName":"root", "password":"root123" }
## Maven插件用法不可用,后面補充吧......
mvn jasypt:encrypt -Djasypt.encryptor.password="myPass"
可能會報如下錯誤,表示本地環(huán)境未安裝JDK的JCE模塊,它是JDK提供的加密擴展,需要到Oracle官網手動下載,并安裝:
下載地址【https://www.oracle.com/technetwork/cn/java/javase/downloads/jce8-download-2133166-zhs.html】
[ERROR] Failed to execute goal com.github.ulisesbocchio:jasypt-maven-plugin:3.0.3:encrypt (default-cli) on project ftp-demo: Error Encrypting: 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 -> [Help 1]
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
java中ConcurrentHashMap的讀操作為什么不需要加鎖
ConcurrentHashMap完全允許多個讀操作并發(fā)進行,讀操作并不需要加鎖。所以下面這篇文章主要給大家介紹了關于java中ConcurrentHashMap的讀操作為什么不需要加鎖的相關資料,需要的朋友可以參考下2018-10-10java使用TimeZone將中國標準時間轉成時區(qū)值
這篇文章主要介紹了java使用TimeZone將中國標準時間轉成時區(qū)值的相關資料,需要的朋友可以參考下2023-11-11SpringBoot整合Echarts實現(xiàn)數(shù)據(jù)大屏
這篇文章給大家介紹了三步實現(xiàn)SpringBoot全局日志記錄,整合Echarts實現(xiàn)數(shù)據(jù)大屏,文中通過代碼示例給大家介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下2024-03-03springboot-2.3.x最新版源碼閱讀環(huán)境搭建(基于gradle構建)
這篇文章主要介紹了springboot-2.3.x最新版源碼閱讀環(huán)境搭建(基于gradle構建),需要的朋友可以參考下2020-08-08Springboot中使用攔截器、過濾器、監(jiān)聽器的流程分析
Javaweb三大組件:servlet、Filter(過濾器)、?Listener(監(jiān)聽器),這篇文章主要介紹了Springboot中使用攔截器、過濾器、監(jiān)聽器的流程分析,感興趣的朋友跟隨小編一起看看吧2023-12-12