|
|
|
@ -7,26 +7,37 @@ import cn.hutool.jwt.JWT;
|
|
|
|
|
import cn.hutool.jwt.JWTUtil;
|
|
|
|
|
import com.supervision.domain.UserInfo;
|
|
|
|
|
import com.supervision.exception.BusinessException;
|
|
|
|
|
import com.supervision.util.SpringBeanUtil;
|
|
|
|
|
import com.supervision.util.TokenUtil;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class JwtInterceptor implements HandlerInterceptor {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
|
|
|
|
//请求消息头获取用户ID
|
|
|
|
|
String token = request.getHeader("token");
|
|
|
|
|
// 如果是开发环境,不获取token
|
|
|
|
|
if (StrUtil.isBlank(token)) {
|
|
|
|
|
throw new BusinessException("当前用户未登录");
|
|
|
|
|
if (StrUtil.isBlank(token) ) {
|
|
|
|
|
// 如果是swagger来的接口,说明这里是测试的,会伪造一个用户
|
|
|
|
|
if (StrUtil.isNotBlank(request.getHeader("Knife4j-Gateway-Code"))){
|
|
|
|
|
token = devActiveUser();
|
|
|
|
|
}else {
|
|
|
|
|
throw new BusinessException("当前用户未登录");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JWT jwt = JWTUtil.parseToken(token);
|
|
|
|
|
// 校验token是否过期,如果过期了,需要提示过期重新登录
|
|
|
|
|
checkTokenExpire(jwt);
|
|
|
|
@ -46,7 +57,6 @@ public class JwtInterceptor implements HandlerInterceptor {
|
|
|
|
|
Object expireTime = jwt.getPayload("expireTime");
|
|
|
|
|
long l = Long.parseLong(String.valueOf(expireTime));
|
|
|
|
|
// 校验是否比当前时间大
|
|
|
|
|
System.out.println(l);
|
|
|
|
|
long currentTimeMillis = System.currentTimeMillis();
|
|
|
|
|
if (currentTimeMillis > l) {
|
|
|
|
|
throw new BusinessException("用户登录已过期,请重新登录");
|
|
|
|
@ -62,6 +72,14 @@ public class JwtInterceptor implements HandlerInterceptor {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String devActiveUser(){
|
|
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
|
|
map.put("id","1");
|
|
|
|
|
map.put("account","test");
|
|
|
|
|
map.put("name","测试账户");
|
|
|
|
|
return TokenUtil.creatToken(JSONUtil.toJsonStr(map));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void clearAuth() {
|
|
|
|
|
ThreadCache.USER.remove();
|
|
|
|
|
}
|
|
|
|
|