zhaoqiaobo
10 months ago
3 changed files with 166 additions and 74 deletions
@ -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; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -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、引入依赖: |
|
||||||
* <dependency> |
|
||||||
* <groupId>org.springframework.cloud</groupId> |
|
||||||
* <artifactId>spring-cloud-commons</artifactId> |
|
||||||
* <version>${version}</version> |
|
||||||
* </dependency> |
|
||||||
* |
|
||||||
* 2、配置文件,或者容器启动变量 |
|
||||||
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.' |
|
||||||
* |
|
||||||
* 3、获取IP |
|
||||||
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); |
|
||||||
*/ |
|
||||||
|
|
||||||
|
|
||||||
} |
|
@ -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<String, String> 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); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue