package com.yeshi.fanli.aspect;
|
|
import java.io.IOException;
|
import java.io.PrintWriter;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.annotation.Around;
|
import org.aspectj.lang.annotation.Aspect;
|
import org.springframework.core.annotation.Order;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
import org.yeshi.utils.JsonUtil;
|
|
import com.yeshi.fanli.dto.WXMPAcceptData;
|
import com.yeshi.fanli.entity.accept.AcceptData;
|
import com.yeshi.fanli.log.LogHelper;
|
import com.yeshi.fanli.util.Constant;
|
import com.yeshi.fanli.util.StringUtil;
|
import com.yeshi.fanli.util.ThreadUtil;
|
|
import net.sf.json.JSONObject;
|
|
/**
|
* 微信小程序接口签名验证
|
*
|
* @author Administrator
|
*
|
*/
|
@Component
|
@Aspect
|
@Order(2)
|
public class WXMPSignValidateAspect {
|
public static final String EDP = "execution(* com.yeshi.fanli.controller.wxmp.*.*.*(..))";
|
|
public static String KEY = "";
|
|
static {
|
KEY = Constant.systemCommonConfig.getSignKey();
|
}
|
|
@Around(EDP)
|
public Object testAround(ProceedingJoinPoint joinPoint) throws IOException {
|
Object[] args = joinPoint.getArgs();
|
PrintWriter out = null;
|
ServletRequestAttributes servletContainer = (ServletRequestAttributes) RequestContextHolder
|
.getRequestAttributes();
|
|
out = servletContainer.getResponse().getWriter();
|
HttpServletRequest request = servletContainer.getRequest();
|
|
WXMPAcceptData acceptData = null;
|
for (Object obj : args) {
|
if (obj instanceof WXMPAcceptData) {
|
acceptData = (WXMPAcceptData) obj;
|
} else if (obj instanceof HttpServletRequest) {
|
request = (HttpServletRequest) obj;
|
}
|
}
|
boolean isRight = true;
|
if (acceptData == null) {
|
out.print(JsonUtil.loadFalseResult(-1, "签名错误"));
|
return null;
|
}
|
|
isRight = signIsRight(request);
|
|
// 签名是否正确
|
if (isRight) {
|
// 判断签名超时
|
if (Math.abs((acceptData.getTimeStamp()) - System.currentTimeMillis()) > 1000 * 60 * 10) {
|
JSONObject data = new JSONObject();
|
data.put("code", -2);
|
data.put("msg", "时间错误");
|
out.print(data);
|
out.close();
|
return null;
|
}
|
|
final String url = request.getRequestURI();
|
@SuppressWarnings("unchecked")
|
final Map<String, Object> params = request.getParameterMap();
|
ThreadUtil.run(new Runnable() {
|
@Override
|
public void run() {
|
// 记录请求日志
|
LogHelper.requestInfo(url, params);
|
}
|
});
|
|
// 设置device
|
if (!StringUtil.isNullOrEmpty(acceptData.getOpenId())) {
|
acceptData.setDevice(acceptData.getAppId() + "-" + acceptData.getOpenId());
|
}
|
|
Object obj = null;
|
try {
|
long startTime = System.currentTimeMillis();
|
obj = joinPoint.proceed(args);
|
final long responseTime = System.currentTimeMillis() - startTime;
|
// 记录大于2s的请求
|
if (responseTime >= 2000) {
|
ThreadUtil.run(new Runnable() {
|
|
@Override
|
public void run() {
|
LogHelper.requestTime(url, params, responseTime);
|
}
|
});
|
|
}
|
|
} catch (Throwable e) {
|
LogHelper.errorDetailInfo(e, getHttpServletParams(request), request.getRequestURI().toString());
|
out.print(JsonUtil.loadFalseResult(90009, "服务器内部错误"));
|
}
|
return obj;
|
} else {
|
JSONObject data = new JSONObject();
|
data.put("code", -1);
|
data.put("msg", "签名错误");
|
out.print(data);
|
out.close();
|
LogHelper.error("签名错误:" + request.getRequestURI() + "-" + getHttpServletParams(request));
|
return null;
|
}
|
}
|
|
/**
|
*
|
*
|
* @param request
|
* @return
|
*/
|
@SuppressWarnings("unchecked")
|
private boolean signIsRight(HttpServletRequest request) {
|
Map<String, Object> map = request.getParameterMap();
|
Iterator<String> its = map.keySet().iterator();
|
List<String> list = new ArrayList<>();
|
|
while (its.hasNext()) {
|
String key = its.next();
|
if (key.equalsIgnoreCase("sign")) {
|
continue;
|
}
|
Object value = map.get(key);
|
Object[] values = (Object[]) value;
|
list.add(key + "=" + values[0].toString());
|
}
|
Collections.sort(list);
|
String str = "";
|
for (String st : list) {
|
str += st + "&";
|
}
|
String sign = null;
|
|
sign = StringUtil.Md5(str + Constant.WXMP_SIGN_KEY);
|
if (sign.equalsIgnoreCase(request.getParameter("sign") + "")) {
|
return true;
|
} else {
|
return false;
|
}
|
}
|
|
/**
|
* 获取请求参数信息
|
*
|
* @param request
|
* @return
|
*/
|
private String getHttpServletParams(HttpServletRequest request) {
|
if (request == null) {
|
return "";
|
}
|
Map map = request.getParameterMap();
|
if (map != null) {
|
Iterator<String> its = map.keySet().iterator();
|
JSONObject json = new JSONObject();
|
while (its.hasNext()) {
|
String next = its.next();
|
if (map.get(next) != null) {
|
Object[] objects = (Object[]) map.get(next);
|
if (objects != null && objects.length > 0) {
|
json.put(next, objects[0].toString());
|
}
|
}
|
}
|
return json.toString();
|
}
|
return "";
|
}
|
|
}
|