• 周四. 6月 30th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

写一个Request包装类ExternalApiHttpServletRequestWrapper(外部接口请求使用用于解密)

admin

11月 28, 2021
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import lombok.extern.slf4j.Slf4j;

import com.alibaba.fastjson.JSONObject;
import com.jrwp.common.filter.AppApiFilter;
import com.jrwp.common.help.AppApiReuestData;
import com.jrwp.common.utils.AESEncodeUtil;
import com.jrwp.common.utils.JacksonUtil;
import com.jrwp.external.api.request.CommonExternalApiRequestData;
import com.jrwp.external.api.request.ExternalApiReuestData;

@Slf4j
public class ExternalApiHttpServletRequestWrapper extends HttpServletRequestWrapper {
    
    private final byte[] body;

    public ExternalApiHttpServletRequestWrapper(HttpServletRequest request,ExternalApiReuestData eard,String key) throws Exception {
        super(request);
        CommonExternalApiRequestData<JSONObject> temp = new CommonExternalApiRequestData<>();
        temp.setAppId(eard.getAppId());
        temp.setRequestData(JSONObject.parseObject(AESEncodeUtil.decode(eard.getRequestData(), key)));
        //eard.setRequestData();
        this.body = JacksonUtil.toJson(temp).getBytes("UTF-8");
    }
    
    public ExternalApiHttpServletRequestWrapper(HttpServletRequest request,AppApiReuestData eard,String key) throws Exception {
        super(request);
        CommonExternalApiRequestData<JSONObject> temp = new CommonExternalApiRequestData<>();
        temp.setAppId(eard.getToken());
        String xxa=eard.getRequestData();
      //  String encryptResultStr = new String(xxa,"utf-8");
        String xxaa=eard.getToken();
        log.info("解密前的数据:",eard.getRequestData());
        log.info("解密前的数据123:",key);
        String jsonStr = AESEncodeUtil.decode(eard.getRequestData(), key);
     //   String jsonStr = AESEncodeUtil.decode(xxa, key);
        log.info("解密后的数据:",jsonStr);
        temp.setRequestData(JSONObject.parseObject(jsonStr));
        //eard.setRequestData();
        this.body = JacksonUtil.toJson(temp).getBytes("UTF-8");
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }
 
    @Override
    public ServletInputStream getInputStream() throws IOException {
 
        final ByteArrayInputStream bais = new ByteArrayInputStream(body);
 
        return new ServletInputStream() {
 
            @Override
            public int read() throws IOException {
                return bais.read();
            }
 
            @Override
            public boolean isFinished() {
                return false;
            }
 
            @Override
            public boolean isReady() {
                return false;
            }
 
            @Override
            public void setReadListener(ReadListener readListener) {
            }
        };
    }

}

发表评论

您的电子邮箱地址不会被公开。