Springboot接口返回參數(shù)及入?yún)SA加密解密的過程詳解
網(wǎng)上有好多通過aop切面以及自定義的RSA工具類進(jìn)行加密解密的方法,期中的過程繁瑣也不好用,博主研究了一天從網(wǎng)上到了超好用的基于Springboot框架實(shí)現(xiàn)的接口RSA加密解密方式,通過rsa-encrypt-body-spring-boot實(shí)現(xiàn)了對Spring Boot接口返回值、參數(shù)值通過注解的方式自動(dòng)加解密。注意:rsa-encrypt-body-spring-boot是某一個(gè)大神寫的工具類上傳到了maven庫中,大家引用即可
一、引入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 # 是否開啟加密 true or false showLog: true # 是否打印加解密log true or false publicKey: # RSA公鑰 privateKey: # RSA私鑰
補(bǔ)充知識:rsa公鑰私鑰生成命令,在電腦文件夾中打開命令框依次執(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
四、對返回值進(jìn)行加密
@Encrypt @GetMapping("/encryption") public TestBean encryption(){ TestBean testBean = new TestBean(); testBean.setName("shuibo.cn"); testBean.setAge(18); return testBean; }
五、對參數(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)Excel的導(dǎo)入、導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)Excel的導(dǎo)入、導(dǎo)出的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06關(guān)于JDK+Tomcat+eclipse+MyEclipse的配置方法,看這篇夠了
關(guān)于JDK+Tomcat+eclipse+MyEclipse的配置問題,很多朋友都搞不太明白,網(wǎng)上一搜配置方法多種哪種最精簡呢,今天小編給大家分享一篇文章幫助大家快速掌握J(rèn)DK Tomcat eclipse MyEclipse配置技巧,需要的朋友參考下吧2021-06-06spring?boot項(xiàng)目中如何使用nacos作為配置中心
這篇文章主要介紹了spring?boot項(xiàng)目中如何使用nacos作為配置中心問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12Socket結(jié)合線程池使用實(shí)現(xiàn)客戶端和服務(wù)端通信demo
這篇文章主要為大家介紹了Socket結(jié)合線程池的使用來實(shí)現(xiàn)客戶端和服務(wù)端通信實(shí)戰(zhàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03java多線程之Future和FutureTask使用實(shí)例
這篇文章主要介紹了java多線程之Future和FutureTask使用實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09Spring的事務(wù)控制實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了Spring的事務(wù)控制實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07