最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot接口返回參數(shù)及入?yún)SA加密解密的過程詳解

 更新時(shí)間:2023年07月08日 11:26:26   作者:豬頭的彩虹糖  
這篇文章主要介紹了Springboot接口返回參數(shù)及入?yún)SA加密解密,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

網(wǎng)上有好多通過aop切面以及自定義的RSA工具類進(jìn)行加密解密的方法,期中的過程繁瑣也不好用,博主研究了一天從網(wǎng)上到了超好用的基于Springboot框架實(shí)現(xiàn)的接口RSA加密解密方式,通過rsa-encrypt-body-spring-boot實(shí)現(xiàn)了對(duì)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ǔ)充知識(shí):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

四、對(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)文章

最新評(píng)論

和政县| 高平市| 广南县| 封丘县| 保山市| 静海县| 青州市| 凤翔县| 旅游| 杭锦旗| 鄱阳县| 汉寿县| 夏津县| 金寨县| 五原县| 山阳县| 合水县| 恩施市| 滦平县| 南开区| 镇坪县| 南充市| 横峰县| 合阳县| 阳山县| 河北省| 夏津县| 博乐市| 鄢陵县| 疏勒县| 南安市| 武乡县| 威信县| 永定县| 越西县| 东港市| 奉新县| 恭城| 虹口区| 招远市| 健康|