Springboot接口返回參數(shù)及入?yún)SA加密解密的過(guò)程詳解
網(wǎng)上有好多通過(guò)aop切面以及自定義的RSA工具類進(jìn)行加密解密的方法,期中的過(guò)程繁瑣也不好用,博主研究了一天從網(wǎng)上到了超好用的基于Springboot框架實(shí)現(xiàn)的接口RSA加密解密方式,通過(guò)rsa-encrypt-body-spring-boot實(shí)現(xiàn)了對(duì)Spring Boot接口返回值、參數(shù)值通過(guò)注解的方式自動(dòng)加解密。注意:rsa-encrypt-body-spring-boot是某一個(gè)大神寫(xiě)的工具類上傳到了maven庫(kù)中,大家引用即可
一、引入rsa-encrypt-body-spring-boot
<dependency> <groupid>cn.shuibo</groupid> <artifactid>rsa-encrypt-body-spring-boot</artifactid> <version>1.0.1.RELEASE</version> </dependency>
二、啟動(dòng)類Application中添加@EnableSecurity注解
@SpringBootApplication
@EnableCaching
@MapperScan("com.ujia")
@EnableScheduling
@EnableSecurity
public class RestApplication {
public static void main(String[] args) {
SpringApplication.run(RestApplication.class, args);
}
}三、在application.yml或者application.properties中添加RSA公鑰及私鑰
rsa:
encrypt:
open: true # 是否開(kāi)啟加密 true or false
showLog: true # 是否打印加解密log true or false
publicKey: # RSA公鑰
privateKey: # RSA私鑰補(bǔ)充知識(shí):rsa公鑰私鑰生成命令,在電腦文件夾中打開(kāi)命令框依次執(zhí)行
Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:217) at java.security.KeyFactory.generatePrivate(KeyFactory.java:372) at com.hashland.otc.common.util.coder.RSACoder.sign(RSACoder.java:42) at com.hashland.otc.common.util.coder.RSACoder.main(RSACoder.java:306) Caused by: java.security.InvalidKeyException: IOException : algid parse error, not a sequence at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:352) at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:357) at sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:91) at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:75) at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:316) at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:213) ... 3 more
重點(diǎn)注意:生成的私鑰一定要轉(zhuǎn)成pkcs8,否則會(huì)報(bào)錯(cuò)
Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:217)
at java.security.KeyFactory.generatePrivate(KeyFactory.java:372)
at com.hashland.otc.common.util.coder.RSACoder.sign(RSACoder.java:42)
at com.hashland.otc.common.util.coder.RSACoder.main(RSACoder.java:306)
Caused by: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:352)
at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:357)
at sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:91)
at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:75)
at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:316)
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:213)
... 3 more
四、對(duì)返回值進(jìn)行加密
@Encrypt
@GetMapping("/encryption")
public TestBean encryption(){
TestBean testBean = new TestBean();
testBean.setName("shuibo.cn");
testBean.setAge(18);
return testBean;
}五、對(duì)參數(shù)進(jìn)行解密
@Decrypt
@PostMapping("/decryption")
public String Decryption(@RequestBody TestBean testBean){
return testBean.toString();
}返回結(jié)果加密——運(yùn)行結(jié)果:

到此這篇關(guān)于Springboot接口返回參數(shù)以及入?yún)SA加密解密的文章就介紹到這了,更多相關(guān)Springboot接口返回參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Maven中關(guān)于依賴導(dǎo)入不進(jìn)的問(wèn)題
這篇文章主要介紹了解決Maven中關(guān)于依賴導(dǎo)入不進(jìn)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Java基礎(chǔ)知識(shí)之CharArrayReader流的使用
這篇文章主要介紹了Java基礎(chǔ)知識(shí)之CharArrayReader流的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java無(wú)需Office環(huán)境實(shí)現(xiàn)Word轉(zhuǎn)HTML功能
在企業(yè)級(jí)開(kāi)發(fā)、文檔管理系統(tǒng)、在線預(yù)覽等場(chǎng)景中,Word?文檔轉(zhuǎn)換為?HTML?是高頻需求,?本文將從環(huán)境配置、基礎(chǔ)轉(zhuǎn)換、高級(jí)定制等維度,全面講解基于該庫(kù)的?Word?轉(zhuǎn)?HTML?技術(shù)實(shí)現(xiàn),希望對(duì)大家有所幫助2026-05-05
java實(shí)現(xiàn)XML增加元素操作簡(jiǎn)單示例
這篇文章主要介紹了java實(shí)現(xiàn)XML增加元素操作,結(jié)合簡(jiǎn)單實(shí)例形式分析了java針對(duì)xml格式數(shù)據(jù)的讀取、遍歷、創(chuàng)建等操作技巧,需要的朋友可以參考下2017-02-02
SpringBoot實(shí)現(xiàn)接口校驗(yàn)簽名調(diào)用的項(xiàng)目實(shí)踐
在以SpringBoot開(kāi)發(fā)后臺(tái)API接口時(shí),會(huì)存在哪些接口不安全的因素呢?通常如何去解決的呢?本文主要介紹了SpringBoot實(shí)現(xiàn)接口校驗(yàn)簽名調(diào)用的項(xiàng)目實(shí)踐,感興趣的可以了解一下2023-09-09
用Java連接sqlserver數(shù)據(jù)庫(kù)時(shí)候幾個(gè)jar包的區(qū)別分析
這篇文章主要介紹了用Java連接sqlserver數(shù)據(jù)庫(kù)時(shí)候幾個(gè)jar包的區(qū)別分析,需要的朋友可以參考下2014-10-10
spring boot高并發(fā)下耗時(shí)操作的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于spring boot高并發(fā)下耗時(shí)操作的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
IDEA中為SpringBoot項(xiàng)目接入MySQL數(shù)據(jù)庫(kù)的詳細(xì)指南
MySQL作為最流行的開(kāi)源關(guān)系型數(shù)據(jù)庫(kù),與Spring Boot的整合是企業(yè)級(jí)開(kāi)發(fā)的標(biāo)配,本文將手把手教你?在IntelliJ IDEA中為Spring Boot項(xiàng)目接入MySQL數(shù)據(jù)庫(kù)?,有需要的可以了解下2025-05-05

