|
|
@ -9,6 +9,7 @@ import com.epmet.commons.tools.redis.RedisUtils; |
|
|
|
import com.epmet.commons.tools.utils.ConvertUtils; |
|
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
|
import com.epmet.feign.EpmetCommonServiceOpenFeignClient; |
|
|
|
import com.epmet.openapi.constant.RequestParamKeys; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.aspectj.lang.JoinPoint; |
|
|
|
import org.aspectj.lang.annotation.Aspect; |
|
|
@ -31,6 +32,7 @@ import java.lang.reflect.InvocationTargetException; |
|
|
|
import java.lang.reflect.Method; |
|
|
|
import java.lang.reflect.Parameter; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
|
|
|
@ -66,25 +68,40 @@ public class OpenApiRequestCheckAspect { |
|
|
|
Parameter[] parameters = method.getParameters(); |
|
|
|
|
|
|
|
HttpServletRequest request = getRequest(); |
|
|
|
String appId = request.getHeader("AppId"); |
|
|
|
|
|
|
|
Map<String, String> argMap = new HashMap<>(); |
|
|
|
for (int i = 0; i < parameters.length; i++) { |
|
|
|
if (parameters[i].isAnnotationPresent(RequestBody.class)) { |
|
|
|
Map<String, String> argMap; |
|
|
|
try { |
|
|
|
argMap = ConvertUtils.entityToMap(args[i]); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new RenException("验签参数转化发生异常"); |
|
|
|
} |
|
|
|
|
|
|
|
argMap.put(""); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (!OpenApiSignUtils.checkSign(argMap, getSecret(appId))) { |
|
|
|
fillRequestParamsInfoArgMap(argMap, request); |
|
|
|
if (!OpenApiSignUtils.checkSign(argMap, getSecret(argMap.get(RequestParamKeys.APP_ID)))) { |
|
|
|
// 验签失败
|
|
|
|
throw new RenException(EpmetErrorCode.OPEN_API_SIGN_ERROR.getCode()); |
|
|
|
} |
|
|
|
checkRepeatRequest(argMap); |
|
|
|
} |
|
|
|
|
|
|
|
private void fillRequestParamsInfoArgMap(Map<String, String> argMap, HttpServletRequest request) { |
|
|
|
fillRequestParamsInfoArgMap(argMap, request, RequestParamKeys.APP_ID); |
|
|
|
fillRequestParamsInfoArgMap(argMap, request, RequestParamKeys.AUTH_TYPE); |
|
|
|
fillRequestParamsInfoArgMap(argMap, request, RequestParamKeys.NONCE); |
|
|
|
fillRequestParamsInfoArgMap(argMap, request, RequestParamKeys.TIMESTAMP); |
|
|
|
fillRequestParamsInfoArgMap(argMap, request, RequestParamKeys.SIGN); |
|
|
|
} |
|
|
|
|
|
|
|
private void fillRequestParamsInfoArgMap(Map<String, String> argMap, HttpServletRequest request, String paramName) { |
|
|
|
String paramValue = request.getParameter(paramName); |
|
|
|
if (StringUtils.isNotBlank(paramName)) { |
|
|
|
argMap.put(paramName, paramValue); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -93,7 +110,7 @@ public class OpenApiRequestCheckAspect { |
|
|
|
* @param argMap |
|
|
|
*/ |
|
|
|
void checkRepeatRequest(Map<String, String> argMap) { |
|
|
|
String timestampStr = argMap.get("timestamp"); |
|
|
|
String timestampStr = argMap.get(RequestParamKeys.TIMESTAMP); |
|
|
|
if (StringUtils.isBlank(timestampStr)) { |
|
|
|
throw new RenException(EpmetErrorCode.OPEN_API_PARAMS_MISSING.getCode()); |
|
|
|
} |
|
|
@ -104,13 +121,13 @@ public class OpenApiRequestCheckAspect { |
|
|
|
// 只允许1分钟之内的请求,允许服务器之间时差为1分钟
|
|
|
|
throw new RenException(String.format("请求已过时,允许时差为%s ms", requestTimeDiff)); |
|
|
|
} |
|
|
|
String nonce = argMap.get("nonce"); |
|
|
|
String nonce = argMap.get(RequestParamKeys.NONCE); |
|
|
|
String nonceInCache = redisUtils.getString(RedisKeys.getOpenApiNonceKey(nonce)); |
|
|
|
if (StringUtils.isNotBlank(nonceInCache)) { |
|
|
|
throw new RenException("请求重复"); |
|
|
|
} |
|
|
|
//将nonce缓存到redis,有效期1分钟
|
|
|
|
redisUtils.set(RedisKeys.getOpenApiNonceKey(nonce), "1", requestTimeDiff); |
|
|
|
redisUtils.set(RedisKeys.getOpenApiNonceKey(nonce), System.currentTimeMillis(), requestTimeDiff); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|