From f3e29db58c0812bc9f6bd5396e87ceb379d93cf9 Mon Sep 17 00:00:00 2001 From: zhaoqiaobo Date: Fri, 29 Mar 2024 12:53:15 +0800 Subject: [PATCH 1/2] =?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); + } +} From 2f7ecdef4e5123b9a4a270273d2fa0f9c93e4b97 Mon Sep 17 00:00:00 2001 From: zhaoqiaobo Date: Fri, 29 Mar 2024 12:59:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat(all):=20=E5=88=A0=E9=99=A4=E5=BF=97?= =?UTF-8?q?=E9=82=A6=E5=B7=A5=E5=8E=82=E7=9A=84xxljob=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1 删除志邦工厂的xxljob配置 --- .../factorydata/zbom/config/XxlJobConfig.java | 74 ------------------- 1 file changed, 74 deletions(-) delete mode 100644 blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/config/XxlJobConfig.java diff --git a/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/config/XxlJobConfig.java b/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/config/XxlJobConfig.java deleted file mode 100644 index 6a820d469..000000000 --- a/blade-service/logpm-factory-data/logpm-factory-data-zbom/src/main/java/com/logpm/factorydata/zbom/config/XxlJobConfig.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.logpm.factorydata.zbom.config; - -import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -/** - * xxl-job config - * - * @author xuxueli 2017-04-28 - */ -@Configuration(proxyBeanMethods = false) -public class XxlJobConfig { - private final Logger logger = LoggerFactory.getLogger(XxlJobConfig.class); - - @Value("${xxl.job.admin.addresses}") - private String adminAddresses; - - @Value("${xxl.job.executor.appname}") - private String appName; - - @Value("${xxl.job.executor.ip}") - private String ip; - - @Value("${xxl.job.executor.port}") - private int port; - - @Value("${xxl.job.accessToken}") - private String accessToken; - - @Value("${xxl.job.executor.logpath}") - private String logPath; - - @Value("${xxl.job.executor.logretentiondays}") - private int logRetentionDays; - - - @Bean - public XxlJobSpringExecutor xxlJobExecutor() { - logger.info(">>>>>>>>>>> xxl-job config init."); - XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); - xxlJobSpringExecutor.setAdminAddresses(adminAddresses); - xxlJobSpringExecutor.setAppName(appName); - xxlJobSpringExecutor.setIp(ip); - xxlJobSpringExecutor.setPort(port); - xxlJobSpringExecutor.setAccessToken(accessToken); - xxlJobSpringExecutor.setLogPath(logPath); - xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays); - - return xxlJobSpringExecutor; - } - - /** - * 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP; - * - * 1、引入依赖: - * - * org.springframework.cloud - * spring-cloud-commons - * ${version} - * - * - * 2、配置文件,或者容器启动变量 - * spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.' - * - * 3、获取IP - * String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); - */ - - -}