Springboot接口返回参数以及入参RSA加密解密

文章介绍了如何使用rsa-encrypt-body-spring-boot库在SpringBoot应用中实现接口的自动加密解密。通过引入依赖、配置公私钥,以及使用@Encrypt和@Decrypt注解,可以方便地处理返回值和参数的加解密。同时强调了生成PKCS8格式私钥的重要性,以避免加密过程中的错误。
摘要由CSDN通过智能技术生成

网上有好多通过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公钥私钥生成命令,在电脑文件夹中打开命令框依次执行

openssl
# 生成私钥
genrsa -out id_rsa_private 2048
# 生成私钥(把RSA私钥转换成PKCS8格式)
pkcs8 -topk8 -inform PEM -in id_rsa_private -outform pem -nocrypt -out id_rsa_private_pkcs
# 生成公钥
rsa -in id_rsa_private -pubout -out id_rsa_public.pub

重点注意:生成的私钥一定要转成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();
}

返回结果加密——运行结果:

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猪头的彩虹糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值