From f3e29db58c0812bc9f6bd5396e87ceb379d93cf9 Mon Sep 17 00:00:00 2001 From: zhaoqiaobo Date: Fri, 29 Mar 2024 12:53:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(all):=20=E5=BF=97=E9=82=A6=E5=B7=A5?= =?UTF-8?q?=E5=8E=82=E6=8E=A5=E5=8F=97=E6=95=B0=E6=8D=AE=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=9D=83=E9=99=90=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1 增加接受志邦工厂数据时的权限处理 --- .../zbom/config/RequestWrapper.java | 71 ++++++++++++++ .../ZbFactoryAccountsInterceptor.java | 95 +++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/config/RequestWrapper.java create mode 100644 blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/interceptor/ZbFactoryAccountsInterceptor.java diff --git a/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/config/RequestWrapper.java b/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/config/RequestWrapper.java new file mode 100644 index 000000000..47c38a56d --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/config/RequestWrapper.java @@ -0,0 +1,71 @@ +package com.logpm.factorydata.zbom.config; + +import javax.servlet.ReadListener; +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class RequestWrapper extends HttpServletRequestWrapper { + private final String body; + public RequestWrapper(HttpServletRequest request) throws IOException { + super(request); + StringBuilder stringBuilder = new StringBuilder(); + BufferedReader bufferedReader = null; + try { + InputStream inputStream = request.getInputStream(); + if (inputStream != null) { + bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + char[] charBuffer = new char[128]; + int bytesRead = -1; + while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { + stringBuilder.append(charBuffer, 0, bytesRead); + } + } else { + stringBuilder.append(""); + } + } catch (IOException ex) { + throw ex; + } finally { + if (bufferedReader != null) { + try { + bufferedReader.close(); + } catch (IOException ex) { + throw ex; + } + } + } + body = stringBuilder.toString(); + } + + @Override + public ServletInputStream getInputStream() throws IOException { + final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes()); + ServletInputStream servletInputStream = new ServletInputStream() { + public boolean isFinished() { + return false; + } + public boolean isReady() { + return false; + } + public void setReadListener(ReadListener readListener) {} + public int read() throws IOException { + return byteArrayInputStream.read(); + } + }; + return servletInputStream; + + } + @Override + public BufferedReader getReader() throws IOException { + return new BufferedReader(new InputStreamReader(this.getInputStream())); + } + public String getBody() { + return this.body; + } + +} diff --git a/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/interceptor/ZbFactoryAccountsInterceptor.java b/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/interceptor/ZbFactoryAccountsInterceptor.java new file mode 100644 index 000000000..4fb7510e9 --- /dev/null +++ b/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/interceptor/ZbFactoryAccountsInterceptor.java @@ -0,0 +1,95 @@ +package com.logpm.factorydata.zbom.interceptor; + +import com.alibaba.fastjson.JSONObject; +import com.logpm.factorydata.zbom.config.RequestWrapper; +import com.logpm.factorydata.zbom.pros.ZbFactoryProperties; +import com.logpm.factorydata.zbom.util.QmSignUntil; +import lombok.AllArgsConstructor; +import lombok.extern.log4j.Log4j2; +import org.springblade.core.tool.api.R; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.Map; + +@Log4j2 +@AllArgsConstructor +public class ZbFactoryAccountsInterceptor implements HandlerInterceptor { + + private final ZbFactoryProperties zbFactoryProperties; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + log.info("#################preHandle: 志邦权限 验证开始"); + RequestWrapper myRequestWrapper = new RequestWrapper(request); + + String method = request.getParameter("method"); + String timestamp = request.getParameter("timestamp"); + String format = request.getParameter("format"); + String appKey = request.getParameter("app_key"); + String v = request.getParameter("v"); + String sign = request.getParameter("sign"); + String signMethod = request.getParameter("sign_method"); + String customerId = request.getParameter("customerId"); + + String body = myRequestWrapper.getBody(); + JSONObject bodyJson = JSONObject.parseObject(body); + log.info("bodyJson ={}",bodyJson.toString()); + + Map params = new HashMap<>(); + params.put("app_key",appKey); + params.put("customerId",customerId); + params.put("format",format); + params.put("method",method); + params.put("sign_method",signMethod); + params.put("timestamp",timestamp); + params.put("v",v); + + String s = body.replaceAll("\\s+", ""); + log.info("################### params={}",params); + log.info("################### secretkey={}",zbFactoryProperties.getSecretkey()); + log.info("################### signMethod={}",signMethod); + + String newSign = QmSignUntil.signTopRequest(params, zbFactoryProperties.getSecretkey(), signMethod, s); + log.info(" 签名 old {}",sign); + log.info(" 新签名 old {}",newSign); + + if(!newSign.equals(sign)){ + returnJson(response,JSONObject.toJSONString(R.fail(203,"签名认证失败"))); + return false; + } + return true; + } + + + private void returnJson(HttpServletResponse response, String json) { + PrintWriter writer = null; + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/json"); + try { + writer = response.getWriter(); + writer.print(json); + + } catch (IOException e) { + System.out.println(e.getMessage()); + } finally { + if (writer != null) + writer.close(); + } + } + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { + HandlerInterceptor.super.postHandle(request, response, handler, modelAndView); + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + HandlerInterceptor.super.afterCompletion(request, response, handler, ex); + } +}