Springboot接口返回參數(shù)及入?yún)SA加密解密的過程詳解
網(wǎng)上有好多通過aop切面以及自定義的RSA工具類進(jìn)行加密解密的方法,期中的過程繁瑣也不好用,博主研究了一天從網(wǎng)上到了超好用的基于Springboot框架實現(xiàn)的接口RSA加密解密方式,通過rsa-encrypt-body-spring-boot實現(xiàn)了對Spring Boot接口返回值、參數(shù)值通過注解的方式自動加解密。注意:rsa-encrypt-body-spring-boot是某一個大神寫的工具類上傳到了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>
二、啟動類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私鑰
補充知識: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
重點注意:生成的私鑰一定要轉(zhuǎn)成pkcs8,否則會報錯
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é)果加密——運行結(jié)果:
到此這篇關(guān)于Springboot接口返回參數(shù)以及入?yún)SA加密解密的文章就介紹到這了,更多相關(guān)Springboot接口返回參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實現(xiàn)Excel的導(dǎo)入、導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)Excel的導(dǎo)入、導(dǎo)出的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下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-06Socket結(jié)合線程池使用實現(xiàn)客戶端和服務(wù)端通信demo
這篇文章主要為大家介紹了Socket結(jié)合線程池的使用來實現(xiàn)客戶端和服務(wù)端通信實戰(zhàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03