1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Map; import java.util.concurrent.ConcurrentHashMap;
@Slf4j public class SignAuthInterceptor implements HandlerInterceptor { private static final String OAUTH2_GET_TOKEN = "/oauth2/getToken";
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String requestURI = request.getRequestURI(); if (!OAUTH2_GET_TOKEN.equals(requestURI)) { return true; } Map<String, String[]> map = request.getParameterMap(); Map<String, String> param = new ConcurrentHashMap<>(10); for (Map.Entry<String, String[]> entry : map.entrySet()) { String key = entry.getKey(); String[] values = entry.getValue(); for (int i = 0; i < values.length; i++) { String value = values[i]; param.put(key, value); } }
String timestamp = request.getHeader("timestamp"); if (StringUtils.isBlank(timestamp)) { log.info("timestamp不能为空..........."); HttpResponseUtil.writer(response, Result.error("timestamp错误")); return false; }
Long timestampLong = Long.valueOf(timestamp); LocalDateTime localDateTime = timestampToDatetime(timestampLong);
if (localDateTime.isAfter(LocalDateTime.now())) { log.info("timestamp 错误 不能在当前时间之后..........."); HttpResponseUtil.writer(response, Result.error("timestamp错误")); return false; }
Long time = System.currentTimeMillis(); if (Math.abs(timestampLong - time) > 120000) { log.info("timestamp失效..........."); HttpResponseUtil.writer(response, Result.error("请求失效")); return false; }
String sign = request.getHeader("sign"); if (StringUtils.isBlank(sign)) { HttpResponseUtil.writer(response, Result.error("sign 错误")); return false; }
String appId = request.getParameter("appId"); if (StringUtils.isBlank(appId)) { HttpResponseUtil.writer(response, Result.error("appId 错误")); return false; }
param.put("timestamp", timestamp);
boolean result = GenSignUtil.isSignValid(param, sign, appId); if (!result) { log.debug("sign签名校验失败..........."); HttpResponseUtil.writer(response, Result.error("sign签名校验失败")); return false; } log.info("签名校验通过,放行..........."); return true; }
public LocalDateTime timestampToDatetime(long timestamp) { Instant instant = Instant.ofEpochMilli(timestamp); return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); }
@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { }
@Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
|