All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nxyfan.framework.common.safe.DecryptRequestBodyAdvice Maven / Gradle / Ivy

There is a newer version: 1.6.3
Show newest version
package com.nxyfan.framework.common.safe;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import com.nxyfan.framework.common.annotation.CommonApiSafe;
import com.nxyfan.framework.common.util.CommonCryptogramUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;

/** 
 *
 * Describe: 对RequestBody请求的参数进行解密
 * Author: Administrator  
 * Create Time: 2024年4月25日 下午2:25:13 
 * Copyright @ 2024 51LIFE  
 */
@ControllerAdvice
public class DecryptRequestBodyAdvice implements RequestBodyAdvice {

	@Override
    public boolean supports(MethodParameter methodParameter, Type targetType, Class> converterType) {
        if(!methodParameter.hasMethodAnnotation(CommonApiSafe.class)) {
            return false;
        }
        return true;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class> converterType) throws IOException {
        //log.info("【进入-RequestBody-beforeBodyRead】");
        //log.info("【拦截到的请求参数为:】" + inputMessage.toString());
    	//String requestBodyStr = getRequestBodyStr(inputMessage.getBody());
        String requestBodyStr = IoUtil.read(inputMessage.getBody(), CharsetUtil.CHARSET_UTF_8);
        requestBodyStr = requestBodyStr.replaceAll("\\r\\n", "");
        String s = CommonCryptogramUtil.doSm2Decrypt(requestBodyStr);
        // 使用解密后的数据,构造新的读取流
        //InputStream rawInputStream = new ByteArrayInputStream(s.getBytes());
        InputStream rawInputStream = IoUtil.toStream(s, CharsetUtil.CHARSET_UTF_8);
        return new HttpInputMessage() {
            @Override
            public HttpHeaders getHeaders() {
                return inputMessage.getHeaders();
            }
            @Override
            public InputStream getBody() throws IOException {
                return rawInputStream;
            }
        };
    }

    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class> converterType) {
        //log.info("【进入-RequestBody-afterBodyRead】");
        return body;
    }

    @Override
    public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class> converterType) {
        //log.info("【进入-RequestBody-handleEmptyBody】");
        return null;
    }

    /**
     * 
     * Describe: reuqest body流数据转换为String
     * Author: Administrator  
     * Create Time: 2024年4月25日 下午2:30:10   
     * @param inputStream
     * @return String
     * @throws IOException
     */
    public static String getRequestBodyStr(InputStream inputStream) throws IOException {
        StringBuilder builder = new StringBuilder();
        if(!ObjectUtils.isEmpty(inputStream)) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            char[] charBuffer = new char[128];
            int bytesRead = -1;
            while((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                builder.append(charBuffer, 0, bytesRead);
            }
        }else {
            builder.append("");
        }
        return builder.toString();
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy