温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot中接口加密解密统一处理

发布时间:2020-03-25 14:58:09 来源:网络 阅读:3393 作者:nineteens 栏目:编程语言

  我们与客户端的接口互动的时候,为了更高的安全性,我们可能需要对接口加密(请求参数加密,服务端解密)、返回信息加密(服务端加密,客户端解密),但是也不是所有的接口都这样,有些接口可能不需要,我们可以使用注解来轻松达到此要求。

  将接口参数的加密解密和返回信息的加密解密分开,分别定义注解,利用Controller的ControllerAdvice来拦截所有的请求,在其中判断是否需要加密解密,即可达到要求。

  使用方法:使用 DecryptRequest 和 EncryptResponse 注解即可,可以放在Controller的类和方法上,其中一个为false就不执行了。像这样:

  @RestController

  @RequestMapping("/test")

  //@DecryptRequest

  @EncryptResponse

  public class TestController {

  @Autowired

  @Qualifier("rrCrypto")

  private Crypto crypto;

  @DecryptRequest(false)

  @EncryptResponse(false)

  @RequestMapping(value = "/enc" , method = RequestMethod.POST)

  public String enc(@RequestBody String body){

  return crypto.encrypt(body);

  }

  }

  定义参数解密的注解,DecryptRequest。

  /**

  * 解密注解

  *

  *

  加了此注解的接口(true)将进行数据解密操作(post的body) 可

  * 以放在类上,可以放在方法上

  * @author xiongshiyan

  */

  @Target({ElementType.METHOD , ElementType.TYPE})

  @Retention(RetentionPolicy.RUNTIME)

  @Documented

  public @interface DecryptRequest {

  /**

  * 是否对body进行解密

  */

  boolean value() default true;

  }

  定义返回信息加密的注解,EncryptResponse。

  /**

  * 加密注解

  *

  *

  加了此注解的接口(true)将进行数据加密操作

  * 可以放在类上,可以放在方法上

  * @author 熊诗言

  */

  @Target({ElementType.METHOD , ElementType.TYPE})

  @Retention(RetentionPolicy.RUNTIME)

  @Documented

  public @interface EncryptResponse {

  /**

  * 是否对结果加密

  */

  boolean value() default true;

  }

  这两个注解可以放在类和方法上,遵循一样的逻辑,即:类上的注解 && 方法上的注解,一方没有即为true,都为false为false。逻辑主要在 NeedCrypto 中。

  /**

  * 判断是否需要加解密

  * @author xiongshiyan at 2018/8/30 , contact me with email yanshixiong@126.com or phone 15208384257

  */

  class NeedCrypto {

  private NeedCrypto(){}

  /**

  * 是否需要对结果加密

  * 1.类上标注或者方法上标注,并且都为true

  * 2.有一个标注为false就不需要加密

  */

  static boolean needEncrypt(MethodParameter returnType) {

  boolean encrypt = false;

  boolean classPresentAnno = returnType.getContainingClass().isAnnotationPresent(EncryptResponse.class);

  boolean methodPresentAnno = returnType.getMethod().isAnnotationPresent(EncryptResponse.class);

  if(classPresentAnno){

  //类上标注的是否需要加密

  encrypt = returnType.getContainingClass().getAnnotation(EncryptResponse.class).value();

  //类不加密,所有都不加密

  if(!encrypt){

  return false;

  }

  }

  if(methodPresentAnno){

  //方法上标注的是否需要加密

  encrypt = returnType.getMethod().getAnnotation(EncryptResponse.class).value();

  }

  return encrypt;

  }

  /**

  * 是否需要参数解密

  * 1.类上标注或者方法上标注,并且都为true

  * 2.有一个标注为false就不需要解密

  */

  static boolean needDecrypt(MethodParameter parameter) {

  boolean encrypt = false;

  boolean classPresentAnno = parameter.getContainingClass().isAnnotationPresent(DecryptRequest.class);

  boolean methodPresentAnno = parameter.getMethod().isAnnotationPresent(DecryptRequest.class);

  if(classPresentAnno){

  //类上标注的是否需要解密

  encrypt = parameter.getContainingClass().getAnnotation(DecryptRequest.class).value();

  //类不加密,所有都不加密

  if(!encrypt){

  return false;

  }

  }

  if(methodPresentAnno){

  //方法上标注的是否需要解密

  encrypt = parameter.getMethod().getAnnotation(DecryptRequest.class).value();

  }

  return encrypt;

  }

  }

  然后定义ControllerAdvice,对于请求解密的,定义 DecryptRequestBodyAdvice ,实现 RequestBodyAdvice 。

  /**

  * 请求数据接收处理类

  *

  * 对加了@Decrypt的方法的数据进行解密操作

  *

  * 只对 @RequestBody 参数有效

  * @author xiongshiyan

  */

  @ControllerAdvice

  @ConditionalOnProperty(prefix = "spring.crypto.request.decrypt", name = "enabled" , havingValue = "true", matchIfMissing = true)

  public class DecryptRequestBodyAdvice implements RequestBodyAdvice {

  @Value("${spring.crypto.request.decrypt.charset:UTF-8}")

  private String charset = "UTF-8";

  @Autowired

  @Qualifier("rrCrypto")

  private Crypto crypto;

  @Override

  public boolean supports(MethodParameter methodParameter, Type targetType,

  Class> converterType) {

  return true;

  }

  @Override

  public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter,

  Type targetType, Class> converterType) {

  return body;

  }

  @Override

  public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,

  Class> converterType) throws IOException {

  if( NeedCrypto.needDecrypt(parameter) ){

  return new DecryptHttpInputMessage(inputMessage , charset , crypto);

  }

  return inputMessage;

  }

  @Override

  public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,

  Class> converterType) {

  return body;

  }

  }无锡人流多少钱 http://www.bhnnk120.com/

  标上注解 ConditionalOnProperty 表示只有条件为true的时候才开启解密功能,一个配置即可打开或者关闭解密功能。真正的解密逻辑留给 DecryptHttpInputMessage , 它又委托给 Crypto。

  /**

  *

  * @author xiongshiyan

  */

  public class DecryptHttpInputMessage implements HttpInputMessage {

  private HttpInputMessage inputMessage;

  private String charset;

  private Crypto crypto;

  public DecryptHttpInputMessage(HttpInputMessage inputMessage, String charset , Crypto crypto) {

  this.inputMessage = inputMessage;

  this.charset = charset;

  this.crypto = crypto;

  }

  @Override

  public InputStream getBody() throws IOException {

  String content = IoUtil.read(inputMessage.getBody() , charset);

  String decryptBody = crypto.decrypt(content, charset);

  return new ByteArrayInputStream(decryptBody.getBytes(charset));

  }

  @Override

  public HttpHeaders getHeaders() {

  return inputMessage.getHeaders();

  }

  }

  对于返回值加密,定义 EncryptResponseBodyAdvice,实现 ResponseBodyAdvice。

  /**

  * 请求响应处理类

  *

  * 对加了@Encrypt的方法的数据进行加密操作

  *

  * @author 

  *

  */

  @ControllerAdvice

  @ConditionalOnProperty(prefix = "spring.crypto.response.encrypt", name = "enabled" , havingValue = "true", matchIfMissing = true)

  public class EncryptResponseBodyAdvice implements ResponseBodyAdvice


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI