Springboot接口返回參數及入參RSA加密解密的過程詳解
網上有好多通過aop切面以及自定義的RSA工具類進行加密解密的方法,期中的過程繁瑣也不好用,博主研究了一天從網上到了超好用的基于Springboot框架實現的接口RSA加密解密方式,通過rsa-encrypt-body-spring-boot實現了對Spring Boot接口返回值、參數值通過注解的方式自動加解密。注意: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
重點注意:生成的私鑰一定要轉成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
四、對返回值進行加密
@Encrypt @GetMapping("/encryption") public TestBean encryption(){ TestBean testBean = new TestBean(); testBean.setName("shuibo.cn"); testBean.setAge(18); return testBean; }
五、對參數進行解密
@Decrypt @PostMapping("/decryption") public String Decryption(@RequestBody TestBean testBean){ return testBean.toString(); }
返回結果加密——運行結果:
到此這篇關于Springboot接口返回參數以及入參RSA加密解密的文章就介紹到這了,更多相關Springboot接口返回參數內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
關于JDK+Tomcat+eclipse+MyEclipse的配置方法,看這篇夠了
關于JDK+Tomcat+eclipse+MyEclipse的配置問題,很多朋友都搞不太明白,網上一搜配置方法多種哪種最精簡呢,今天小編給大家分享一篇文章幫助大家快速掌握JDK Tomcat eclipse MyEclipse配置技巧,需要的朋友參考下吧2021-06-06