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

在实际项目中,我们常常需要在请求前后进行一些操作,比如:参数解密/返回结果加密,打印请求参数和返回结果的日志等。这些与业务无关的东西,我们不希望写在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 {
}
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算法工具类方法实现就不在这里展示了,网上很容易找到说明。
异常类以及错误码是当前项目自定义的,非重点部分,也不做说明,大家使用时修改为自己逻辑即可。

  • 20
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Integration MQTT 是一个基于 Spring Integration 的 MQTT 客户端,可以轻松地在 Spring Boot 应用程序中使用 MQTT。 以下是 Spring Integration MQTT 的示例代码: 1. 添加 Maven 依赖 在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-mqtt</artifactId> <version>5.3.2.RELEASE</version> </dependency> ``` 2. 创建 Spring Boot 应用程序 创建一个 Spring Boot 应用程序并在 application.yml 文件中添加以下配置: ``` spring: mqtt: url: tcp://localhost:1883 username: admin password: password ``` 3. 创建 MQTT 连接工厂 创建一个 MQTT 连接工厂并设置连接属性: ``` @Bean public MqttPahoClientFactory mqttClientFactory() { DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory(); MqttConnectOptions options = new MqttConnectOptions(); options.setUserName("admin"); options.setPassword("password".toCharArray()); factory.setConnectionOptions(options); return factory; } ``` 4. 创建 MQTT 订阅者 创建一个 MQTT 订阅者并添加到 Spring Integration 流中: ``` @Bean public MessageProducer inbound() { MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("testClient", mqttClientFactory(), "testTopic"); adapter.setCompletionTimeout(5000); adapter.setConverter(new DefaultPahoMessageConverter()); adapter.setQos(1); adapter.setOutputChannel(receiveChannel()); return adapter; } ``` 5. 创建消息处理器 创建一个消息处理器来处理接收到的消息: ``` @ServiceActivator(inputChannel = "receiveChannel") public void receiveMessage(String message) { System.out.println("Received message: " + message); } ``` 6. 创建 MQTT 发布者 创建一个 MQTT 发布者并添加到 Spring Integration 流中: ``` @Bean public MessageChannel mqttOutboundChannel() { return new DirectChannel(); } @Bean @ServiceActivator(inputChannel = "mqttOutboundChannel") public MessageHandler mqttOutbound() { MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler("testClient", mqttClientFactory()); messageHandler.setAsync(true); messageHandler.setDefaultTopic("testTopic"); return messageHandler; } ``` 7. 发布消息 在代码中使用以下代码来发布消息: ``` @Autowired private MessageChannel mqttOutboundChannel; public void sendMessage(String message) { mqttOutboundChannel.send(MessageBuilder.withPayload(message).build()); } ``` 以上就是 Spring Integration MQTT 的示例代码,希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值