Spring Boot 接口参数自动加解密

  • Post author:
  • Post category:其他




本文标题:Spring Boot 接口参数自动加解密


原始链接:

https://www.shuibo.cn/102.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。



1.介绍


rsa-encrypt-body-spring-boot


实现了对Spring Boot接口返回值、参数值通过注解的方式自动加解密。





2.使用方法


Apache Maven

<dependency>
  <groupId>cn.shuibo</groupId>
  <artifactId>rsa-encrypt-body-spring-boot</artifactId>
  <version>1.0.0.RELEASE</version>
</dependency>


Gradle Groovy DSL

implementation 'cn.shuibo:rsa-encrypt-body-spring-boot:1.0.0.RELEASE'


Gradle Kotlin DSL



Scala SBT



Apache Ivy



Groovy Grape



Leiningen



Apache Buildr



Maven Central Badge



PURL



Bazel

方式请阅读

Spring Boot接口RSA自动加解密


  • 以Maven为例,在pom.xml中引入依赖
<dependency>
    <groupId>cn.shuibo</groupId>
    <artifactId>rsa-encrypt-body-spring-boot</artifactId>
    <version>1.0.0.RELEASE</version>
</dependency>

  • 启动类Application中添加@EnableSecurity注解
@SpringBootApplication
@EnableSecurity
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

  • 在application.yml或者application.properties中添加RSA公钥及私钥
rsa:
  encrypt:
    debug: false # true表示开启调试,不加密。(方便开发时测试)
    publicKey: 123456
    privateKey: 123456

  • 对返回值进行加密
@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();
}



3.About author



版权声明:本文为youyuc原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。