|
|
|
@@ -2,6 +2,8 @@ package com.iformall.interceptor; |
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.alibaba.fastjson.TypeReference; |
|
|
|
import com.iformall.common.ErrorCode; |
|
|
|
import com.iformall.domain.po.WxThirdPartyApi; |
|
|
|
import com.iformall.exception.MallinkException; |
|
|
|
@@ -51,6 +53,8 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter { |
|
|
|
@Override |
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
|
|
|
|
|
|
|
response.setHeader("Content-type", "application/json;charset=UTF-8"); |
|
|
|
|
|
|
|
String appkey = request.getHeader("appkey"); |
|
|
|
if (StringUtils.isBlank(appkey) || !appkey.contains("&")) { |
|
|
|
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"非法请求"); |
|
|
|
@@ -68,22 +72,55 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter { |
|
|
|
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"缺少加密串"); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String body = ((BodyReaderHttpServletRequestWrapper) request).getBody(); |
|
|
|
if(StringUtils.isBlank(body)){ |
|
|
|
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"缺少参数"); |
|
|
|
} |
|
|
|
Map<String, String> param = StringToMap(body); |
|
|
|
if(param.isEmpty()){ |
|
|
|
try{ |
|
|
|
// JSONObject parameterMap = JSON.parseObject(body); |
|
|
|
Map<String, String> parameterMap = JSONObject.parseObject(body, new TypeReference<Map<String, String>>() { |
|
|
|
}); |
|
|
|
|
|
|
|
String timeStamp = parameterMap.get("timeStamp"); |
|
|
|
long timestampDate = Long.valueOf(timeStamp) + 1000*60*5;//五分钟有效 |
|
|
|
long currDate = System.currentTimeMillis(); |
|
|
|
// 请求过期 |
|
|
|
if (timestampDate < currDate) { |
|
|
|
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"请求过期"); |
|
|
|
} |
|
|
|
|
|
|
|
logger.info("sign={}"+signature); |
|
|
|
String signKey = apiConfig.getSignKey();//TODO 根据appId查询密钥,缓存 |
|
|
|
String newSignature = SignUtils.getSign(signKey, parameterMap, "MD5"); |
|
|
|
//加密串不匹配 |
|
|
|
if (!signature.equals(newSignature)) { |
|
|
|
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"加密串校验失败"); |
|
|
|
} |
|
|
|
Integer cache = RedisCacheUtils.getCacheInteger(redisTemplate, Constant.publicApiNonce+signature); |
|
|
|
if (null != cache) { |
|
|
|
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"重复调用"); |
|
|
|
} |
|
|
|
RedisCacheUtils.cache(redisTemplate, Constant.publicApiNonce+signature, 1, 300); |
|
|
|
return true; |
|
|
|
|
|
|
|
}catch(Exception e){ |
|
|
|
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"参数格式不正确"); |
|
|
|
} |
|
|
|
|
|
|
|
String timeStamp = param.get("timeStamp"); |
|
|
|
long timestampDate = Long.valueOf(timeStamp) + 1000*60*5;//五分钟有效 |
|
|
|
long currDate = System.currentTimeMillis(); |
|
|
|
// 请求过期 |
|
|
|
if (timestampDate < currDate) { |
|
|
|
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"请求过期"); |
|
|
|
} |
|
|
|
// Map<String, String> param = StringToMap(body); |
|
|
|
// if(param.isEmpty()){ |
|
|
|
// throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"参数格式不正确"); |
|
|
|
// } |
|
|
|
// |
|
|
|
// String timeStamp = param.get("timeStamp"); |
|
|
|
// long timestampDate = Long.valueOf(timeStamp) + 1000*60*5;//五分钟有效 |
|
|
|
// long currDate = System.currentTimeMillis(); |
|
|
|
// // 请求过期 |
|
|
|
// if (timestampDate < currDate) { |
|
|
|
// throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"请求过期"); |
|
|
|
// } |
|
|
|
|
|
|
|
// String nonceStr = param.get("nonceStr"); |
|
|
|
// //重复调用 |
|
|
|
@@ -114,49 +151,49 @@ public class AuthorizationInterceptor extends HandlerInterceptorAdapter { |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
logger.info("sign={}"+signature); |
|
|
|
String signKey = apiConfig.getSignKey();//TODO 根据appId查询密钥,缓存 |
|
|
|
String newSignature = SignUtils.getSign(signKey, param, "MD5"); |
|
|
|
//加密串不匹配 |
|
|
|
if (!signature.equals(newSignature)) { |
|
|
|
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"加密串校验失败"); |
|
|
|
} |
|
|
|
Integer cache = RedisCacheUtils.getCacheInteger(redisTemplate, Constant.publicApiNonce+signature); |
|
|
|
if (null != cache) { |
|
|
|
throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"重复调用"); |
|
|
|
} |
|
|
|
RedisCacheUtils.cache(redisTemplate, Constant.publicApiNonce+signature, 1, 300); |
|
|
|
return true; |
|
|
|
// logger.info("sign={}"+signature); |
|
|
|
// String signKey = apiConfig.getSignKey();//TODO 根据appId查询密钥,缓存 |
|
|
|
// String newSignature = SignUtils.getSign(signKey, param, "MD5"); |
|
|
|
// //加密串不匹配 |
|
|
|
// if (!signature.equals(newSignature)) { |
|
|
|
// throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"加密串校验失败"); |
|
|
|
// } |
|
|
|
// Integer cache = RedisCacheUtils.getCacheInteger(redisTemplate, Constant.publicApiNonce+signature); |
|
|
|
// if (null != cache) { |
|
|
|
// throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"重复调用"); |
|
|
|
// } |
|
|
|
// RedisCacheUtils.cache(redisTemplate, Constant.publicApiNonce+signature, 1, 300); |
|
|
|
// return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private Map<String, String> StringToMap(String body){ |
|
|
|
Map<String, String> map = new HashMap<String, String>(); |
|
|
|
String bodyDecode = ""; |
|
|
|
try { |
|
|
|
bodyDecode = URLDecoder.decode(body, "utf-8"); |
|
|
|
} catch (UnsupportedEncodingException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return map; |
|
|
|
} |
|
|
|
|
|
|
|
String[] keyValues = bodyDecode.split("&"); |
|
|
|
for (String kv:keyValues) { |
|
|
|
if(StringUtils.isBlank(kv) || !kv.contains("=")){ |
|
|
|
continue; |
|
|
|
} |
|
|
|
String[] key_value = kv.split("="); |
|
|
|
String key = key_value[0]; |
|
|
|
if(StringUtils.isNotBlank(key) |
|
|
|
&& !"sign".equals(key) |
|
|
|
&& !"appId".equals(key) |
|
|
|
&& !"appKey".equals(key) |
|
|
|
&& !"signKey".equals(key)){ |
|
|
|
String value = kv.substring(kv.indexOf('=') + 1, kv.length()); |
|
|
|
map.put(key_value[0],value); |
|
|
|
} |
|
|
|
} |
|
|
|
return map; |
|
|
|
} |
|
|
|
// private Map<String, String> StringToMap(String body){ |
|
|
|
// Map<String, String> map = new HashMap<String, String>(); |
|
|
|
// String bodyDecode = ""; |
|
|
|
// try { |
|
|
|
// bodyDecode = URLDecoder.decode(body, "utf-8"); |
|
|
|
// } catch (UnsupportedEncodingException e) { |
|
|
|
// e.printStackTrace(); |
|
|
|
// return map; |
|
|
|
// } |
|
|
|
// |
|
|
|
// String[] keyValues = bodyDecode.split("&"); |
|
|
|
// for (String kv:keyValues) { |
|
|
|
// if(StringUtils.isBlank(kv) || !kv.contains("=")){ |
|
|
|
// continue; |
|
|
|
// } |
|
|
|
// String[] key_value = kv.split("="); |
|
|
|
// String key = key_value[0]; |
|
|
|
// if(StringUtils.isNotBlank(key) |
|
|
|
// && !"sign".equals(key) |
|
|
|
// && !"appId".equals(key) |
|
|
|
// && !"appKey".equals(key) |
|
|
|
// && !"signKey".equals(key)){ |
|
|
|
// String value = kv.substring(kv.indexOf('=') + 1, kv.length()); |
|
|
|
// map.put(key_value[0],value); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// return map; |
|
|
|
// } |
|
|
|
|
|
|
|
} |