|
@@ -1,8 +1,6 @@
|
|
|
package cn.rankin.apiweb.intercepter;
|
|
|
|
|
|
import cn.rankin.apiweb.code.ApiWebCode;
|
|
|
-import cn.rankin.apiweb.security.JwsToken;
|
|
|
-import cn.rankin.apiweb.security.JwsTokenService;
|
|
|
import cn.rankin.apiweb.vo.DeviceUserVo;
|
|
|
import cn.rankin.common.utils.api.model.BaseCode;
|
|
|
import cn.rankin.common.utils.constant.RedisKey;
|
|
@@ -12,6 +10,7 @@ import com.alibaba.fastjson.JSON;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.core.NamedThreadLocal;
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
import org.springframework.stereotype.Component;
|
|
@@ -21,6 +20,8 @@ import org.springframework.web.servlet.ModelAndView;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
|
|
|
@Component
|
|
|
public class LoginInterceptor implements HandlerInterceptor {
|
|
@@ -33,17 +34,15 @@ public class LoginInterceptor implements HandlerInterceptor {
|
|
|
|
|
|
private NamedThreadLocal<Long> startTimeThreadLocal = new NamedThreadLocal<Long>("StopWatch-StartTime");
|
|
|
|
|
|
- private static final long REFRESH_INTERVAL = 2 * 60 * 60 * 1000;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- RedisService<String, Object> redisService;
|
|
|
-
|
|
|
@Autowired
|
|
|
- private JwsTokenService jwsTokenService;
|
|
|
+ private RedisService<String, Object> redisService;
|
|
|
|
|
|
// 忽略options请求,默认为true
|
|
|
private boolean ignoreOptions = true;
|
|
|
|
|
|
+ @Value("${'${request.header.ignore_path}'.split(',')}")
|
|
|
+ private List<String> ignorePaths = new ArrayList<>();
|
|
|
+
|
|
|
@Override
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
|
|
@@ -59,39 +58,31 @@ public class LoginInterceptor implements HandlerInterceptor {
|
|
|
startTimeThreadLocal.set(System.currentTimeMillis());//线程绑定变量(该数据只有当前请求的线程可见)
|
|
|
|
|
|
//登录请求不拦截
|
|
|
- String url = request.getServletPath();
|
|
|
- if (url.equals("/login") || url.equals("/login/")) {
|
|
|
+ String path = request.getServletPath();
|
|
|
+ if (ignorePaths.contains(path)) {
|
|
|
+ logger.info("url: {} not intercepted!");
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- String token = request.getHeader("Authentication");
|
|
|
- logger.info("请求开始 url={} token={}", url, token);
|
|
|
- if (StringUtils.isEmpty(token)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
+ String uid = request.getHeader("uid");
|
|
|
+ String eid = request.getHeader("eid");
|
|
|
+ String sign = request.getHeader("sign");
|
|
|
+ String requestId = request.getHeader("requestId");
|
|
|
+ logger.info("request start, requestId={}, path={}, uid={}, eid={}, sign={}", requestId, path, uid, eid, sign);
|
|
|
|
|
|
- JwsToken jwsToken = jwsTokenService.parse(token);
|
|
|
- if (jwsToken == null) {
|
|
|
+ if (StringUtils.isEmpty(uid) || StringUtils.isEmpty(sign)) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- String userName = jwsToken.getUserName();
|
|
|
-
|
|
|
//因为缓存了用户id和设备id
|
|
|
- DeviceUserVo du = (DeviceUserVo) redisService.get(String.format(LOGIN_TOKEN_FORMAT_KEY, userName));
|
|
|
+ DeviceUserVo du = (DeviceUserVo) redisService.get(String.format(LOGIN_TOKEN_FORMAT_KEY, uid));
|
|
|
if (null == du) {
|
|
|
- logger.error("验证 token 异常: token={}", JSON.toJSONString(jwsToken));
|
|
|
+ logger.error("check header failed, not exists!");
|
|
|
request.setAttribute(ERROR_LOGIN_HEADER, "ERROR_TOKEN");
|
|
|
responseOutWithJson(request, response);
|
|
|
return false;
|
|
|
- }else if (!jwsToken.getDeviceId().equals(du.getDevice())) {
|
|
|
- logger.error("device not match, token={}", JSON.toJSONString(jwsToken));
|
|
|
- request.setAttribute(ERROR_LOGIN_HEADER, "NOT_MATCH");
|
|
|
- responseOutWithJson(request, response);
|
|
|
- return false;
|
|
|
}
|
|
|
- //查询到结果 如果存在 token 对应的 vo
|
|
|
- // 1.判断 如果有效期小于 2小时
|
|
|
+
|
|
|
logger.info("token check success: {}", JSON.toJSONString(du));
|
|
|
|
|
|
return true;
|