首页 > 其他分享 >RequestBodyAdvice用法详解-参数加解密示例

RequestBodyAdvice用法详解-参数加解密示例

时间:2024-05-09 19:55:56浏览次数:12  
标签:lang return RequestBodyAdvice java 示例 加解密 import com annotation

 

在实际项目中,我们常常需要在请求前后进行一些操作,比如:参数解密/返回结果加密,打印请求参数和返回结果的日志等。这些与业务无关的东西,我们不希望写在controller方法中,造成代码重复可读性变差。这里,我们讲讲使用@ControllerAdvice和RequestBodyAdvice、ResponseBodyAdvice来对请求前后进行处理(本质上就是AOP),来实现日志记录每一个请求的参数和返回结果。

实现步骤
1、声明一个类型,加上@ControllerAdvice注解,类实现接口RequestBodyAdvice,如果不想去重写所有的接口方法,也可以继承spring给提供的抽象类RequestBodyAdviceAdapter。

如下:

package com.spring.project;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jd.healthy.connector.common.annotations.ExcludeHttpBodyDecrypt;
import com.spring.project.common.annotations.HttpBodyDecrypt;
import com.spring.project.common.exception.BusinessException;
import com.spring.project.common.result.ResultCode;
import com.spring.project.common.util.AESUtil;
import com.spring.project.common.util.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;

/**
* RequestBodyHandler
*
* @Author jisiqian
* @Date 2021/8/15 12:58
*/
@RestControllerAdvice
@Slf4j
public class RequestBodyHandler implements RequestBodyAdvice {

@Value("${http.body.AESKey}")
public String AESKey;

@Value("${http.body.MD5Key}")
public String MD5Key;

/**
* 该方法用于判断当前请求,是否要执行beforeBodyRead方法
* methodParameter方法的参数对象
* type方法的参数类型
* aClass 将会使用到的Http消息转换器类类型
* 注意:此判断方法,会在beforeBodyRead 和 afterBodyRead方法前都触发一次。
*
* @return 返回true则会执行beforeBodyRead
*/
@Override
public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
//排除解密注解
boolean methodHasExcludeHttpBodyDecrypt = methodParameter.hasMethodAnnotation(ExcludeHttpBodyDecrypt.class);
if (methodHasExcludeHttpBodyDecrypt) {
return false;
}
//解密注解
boolean methodHasHttpBodyDecrypt = methodParameter.hasMethodAnnotation(HttpBodyDecrypt.class);
if (methodHasHttpBodyDecrypt) {
return true;
}
boolean classHasExcludeHttpBodyDecrypt = methodParameter.getDeclaringClass().getAnnotation(ExcludeHttpBodyDecrypt.class) != null;
if (classHasExcludeHttpBodyDecrypt) {
return false;
}
boolean classHasHttpBodyDecrypt = methodParameter.getDeclaringClass().getAnnotation(HttpBodyDecrypt.class) != null;
if (classHasHttpBodyDecrypt) {
return true;
}
return false;
}

/**
* 在Http消息转换器执转换,之前执行
* inputMessage 客户端的请求数据
* parameter方法的参数对象
* targetType方法的参数类型
* converterType 将会使用到的Http消息转换器类类型
* @return 返回 一个自定义的HttpInputMessage
*/
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
if (inputMessage.getBody().available()<=0) {
return inputMessage;
}
byte[] requestDataByte=new byte[inputMessage.getBody().available()];
inputMessage.getBody().read(requestDataByte);
byte[] requestDataByteNew=null;
try {
// 解密
requestDataByteNew = this.decryptHandler(requestDataByte);
} catch (Exception e) {
throw e;
}
// 使用解密后的数据,构造新的读取流
InputStream rawInputStream = new ByteArrayInputStream(requestDataByteNew);
return new HttpInputMessage() {
@Override
public HttpHeaders getHeaders() {
return inputMessage.getHeaders();
}

@Override
public InputStream getBody() throws IOException {
return rawInputStream;
}
};
}

/**
* 在Http消息转换器执转换,之后执行
* body 转换后的对象
* inputMessage 客户端的请求数据
* parameter handler方法的参数类型
* targetType handler方法的参数类型
* converterType 使用的Http消息转换器类类型
* @return 返回一个新的对象
*/
@Override
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return body;
}

/**
* 参数与afterBodyRead相同,不过这个方法处理的是,body为空的情况
*/
@Override
public Object handleEmptyBody(@Nullable Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
return body;
}

/**
* AES解密 加MD5签名验证。
* @param requestBytes
* @return
*/
public byte[] decryptHandler(byte[] requestBytes) throws UnsupportedEncodingException {
if (requestBytes.length <= 0) {
return new byte[0];
}
String requestData = new String(requestBytes, StandardCharsets.UTF_8);
JSONObject jsonobj = JSON.parseObject(requestData);
String encrypt= jsonobj.get("encrypt")==null?"":jsonobj.get("encrypt").toString();
String decrypt = "";
if (encrypt.length() > 0) {
//解密参数
decrypt = AESUtil.AESDecode(encrypt, AESKey);
if (decrypt == null) {
//解密失败,返回异常
throw new BusinessException(ResultCode.PARAM_FUNCTION_IS_ILLEGAL);
}
}

//获取并验证签名。
String sign = jsonobj.get("sign")==null?"":jsonobj.get("sign").toString();
String md5Data = MD5Util.MD5(decrypt+MD5Key);
if (!sign.equals(md5Data)) {
//验签失败
throw new BusinessException(ResultCode.PARAM_FUNCTION_IS_ILLEGAL);
}
//验证通过,返回解密后的参数
return decrypt.getBytes(StandardCharsets.UTF_8);
}

}



package com.spring.project;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* RequestBodyHandler
*
* @Author jisiqian
* @Date 2021/8/15 12:58
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ExcludeHttpBodyDecrypt {
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.spring.project;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* RequestBodyHandler
*
* @Author jisiqian
* @Date 2021/8/15 12:58
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface HttpBodyDecrypt {
}

有一点需要注意:
supports判断方法,会在beforeBodyRead 和 afterBodyRead方法前都触发一次,都会对应根据返回值要不要继续执行beforeBodyRead 或afterBodyRead。

上述示例是在请求参数统一进行解密和验证签名操作。
其中参数采用AES加密解密,签名适用MD5加密算法得到信息摘要。
具体加解密和MD5算法工具类方法实现就不在这里展示了,网上很容易找到说明。
异常类以及错误码是当前项目自定义的,非重点部分,也不做说明,大家使用时修改为自己逻辑即可

标签:lang,return,RequestBodyAdvice,java,示例,加解密,import,com,annotation
From: https://www.cnblogs.com/zhoading/p/18182977

相关文章

  • 要获取线程池中任务的返回值,可以使用submit()方法返回的Future对象。你可以通过调用Fu
    importjava.util.concurrent.ArrayBlockingQueue;importjava.util.concurrent.ThreadPoolExecutor;importjava.util.concurrent.TimeUnit;publicclassMain{publicstaticvoidmain(String[]args){//设置线程池参数intcorePoolSize=5;//......
  • 基于nodeje的RSA加解密
    RAS是一种非对称加密,可以用公钥加密,私钥解密也可以反过来用私钥加密,公钥解密;以下是其实现方式,与java后台匹配,实现双向加解密。/***RSA最大加密明文大小*/constMAX_ENCRYPT_BLOCK=245;/***RSA最大解密密文大小*/constMAX_DECRYPT_BLOCK=256;通过fs.readFil......
  • dubbo 泛型调用示例 (dubbo generic call)
     1.背景泛型调用适用于观察者模式,即有很多广泛的消费者,但生产者又不想依赖消费者的client包,比如常见的API开放平台的回调机制; 2.泛型调用要实现泛型调用,几个核心点:泛型入参如何构建泛型服务service如何构建泛型调用结果如何拿到 2.1泛型入参 泛型入参须是Ha......
  • Java护照识别接口开发示例、文字识别、证件识别
    护照是我们出国旅行时所必要的证件之一,他是我国公民去外国的旅行和工作的时候所代表的一个合法的身份证件。在护照上面也有不少关于我们个人身份的信息,而手动去录入如此多的身份信息这绝对是灾难。不仅证件,有的场景还需要录入很多文字信息。翔云API可识别图片上的身份证、护......
  • NodeJS路径遍历:示例及预防
    让我们来看看什么是路径遍历攻击,以及在Node.js中可以采用哪些方法来阻止这种攻击。构建一个安全而健壮的应用程序需要考虑的因素很多,并非一件容易的事情。要确保覆盖所有潜在的漏洞是一项十分艰巨的任务,这需要大量的经验和指导。在这些漏洞中,有一个和系统目录访问安全相......
  • 非对称加密中,加解密和签名
    在非对称加密中,加解密使用的密钥取决于具体的用途:加密:通常情况下,当想要确保数据的机密性,即希望只有特定接收方能够读取信息时,发送方会使用接收方的公钥对数据进行加密。这样一来,只有拥有对应私钥的接收方才能够解密并查看原始信息。解密:对应地,接收方收到加密后的数据后,......
  • openGauss 并发写入示例
    并发写入示例本章节以表test为例,分别介绍相同表的INSERT和DELETE并发,相同表的并发INSERT,相同表的并发UPDATE,以及数据导入和查询的并发的执行详情。CREATETABLEtest(idint,namechar(50),addressvarchar(255));相同表的INSERT和DELETE并发相同表的并发INSERT相同......
  • LLM2Vec介绍和将Llama 3转换为嵌入模型代码示例
    嵌入模型是大型语言模型检索增强生成(RAG)的关键组成部分。它们对知识库和用户编写的查询进行编码。使用与LLM相同领域的训练或微调的嵌入模型可以显著改进RAG系统。然而,寻找或训练这样的嵌入模型往往是一项困难的任务,因为领域内的数据通常是稀缺的。但是这篇论文LLM2Vec,可以将......
  • 窗口程序框架示例代码
    #include<windows.h>//创建窗口程序的步骤://1.创建WinMain()主函数//2.设计窗口//3.注册窗口//4.创建窗口//5.显示窗口//回调函数,消息处理函数LRESULTCALLBACKWndProc(HWNDhwnd,//窗口句柄 UINTMessage,//消息 WPARAMwParam,//消息参数 ......
  • 大模型_2.2:Prompt示例
    1、行业洞察分析  行业洞察分析的方法论:使用麦肯锡工作法可以快速了解一个行业。Step1:通过搜索分析几十个与该行业相关的关键词,覆盖该行业的上下游,以获取全面的信息。Step2:阅读一些行业最新的研究分析报告,这些报告通常包含了行业趋势、市场规模、竞争格局等重要信息。Step......