|
|
@@ -1,7 +1,14 @@
|
|
|
package com.coffee.bus.web.handler;
|
|
|
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.crypto.digest.DigestUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import com.coffee.bus.web.RequestCheckProperties;
|
|
|
import com.coffee.common.exception.RequestParmErrorException;
|
|
|
+import com.coffee.common.exception.RequestSignErrorException;
|
|
|
+import com.coffee.common.exception.RequestTimeOutException;
|
|
|
+import com.coffee.common.redis.RedisUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
@@ -11,6 +18,8 @@ import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* @author lifang
|
|
|
@@ -24,6 +33,8 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
@Order(10)
|
|
|
@AllArgsConstructor
|
|
|
public class CheckRequestHandler implements HandlerInterceptor {
|
|
|
+ private final RequestCheckProperties properties;
|
|
|
+ private final RedisUtils redisUtils;
|
|
|
@Override
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
String method = request.getMethod();
|
|
|
@@ -32,17 +43,51 @@ public class CheckRequestHandler implements HandlerInterceptor {
|
|
|
String sign = request.getHeader("Sign");
|
|
|
String timestamp = request.getHeader("Timestamp");
|
|
|
String authorization = request.getHeader("Authorization");
|
|
|
+ String body = IoUtil.read(request.getInputStream(), Charset.defaultCharset());
|
|
|
if(StrUtil.isBlank(sign)
|
|
|
- ||StrUtil.isBlank(timestamp)
|
|
|
- ||StrUtil.isBlank(authorization)){
|
|
|
+ ||StrUtil.isBlank(timestamp)
|
|
|
+ ||StrUtil.isBlank(authorization)){
|
|
|
throw new RequestParmErrorException();
|
|
|
}
|
|
|
try {
|
|
|
- Long.valueOf(timestamp);
|
|
|
- }catch (Exception e){
|
|
|
+ checkTime( Long.valueOf(timestamp));
|
|
|
+ }catch (NumberFormatException e){
|
|
|
throw new RequestParmErrorException();
|
|
|
}
|
|
|
+ checkSign(sign,timestamp,authorization,body);
|
|
|
+// checkRepeat(sign);
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ private void checkTime(Long timestamp){
|
|
|
+ long now = System.currentTimeMillis();
|
|
|
+ //时间间隔不超10分钟
|
|
|
+ long subTime = TimeUnit.MILLISECONDS
|
|
|
+ .toSeconds(Math.abs(Math.subtractExact(timestamp,now)));
|
|
|
+ if(subTime>properties.getExpireInterval()){
|
|
|
+ throw new RequestTimeOutException();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkSign(String sign,String timestamp,String authorization,String body){
|
|
|
+ JSONObject jsonObject = new JSONObject(true);
|
|
|
+ jsonObject.putOpt("body",body);
|
|
|
+ jsonObject.putOpt("timestamp",timestamp);
|
|
|
+ jsonObject.putOpt("token",authorization);
|
|
|
+ String encodeSign = DigestUtil.md5Hex(jsonObject.toString());
|
|
|
+ if (!encodeSign.equals(sign)) {
|
|
|
+ throw new RequestSignErrorException();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// private void checkRepeat(String sign){
|
|
|
+// Object result = redisUtils.get(sign);
|
|
|
+// if(result==null){
|
|
|
+// //请求60秒过期
|
|
|
+// redisUtils.set(sign,1,properties.getRepeatInterval());
|
|
|
+// }else {
|
|
|
+// throw new RequestRepeatException();
|
|
|
+// }
|
|
|
+// }
|
|
|
}
|