|
@@ -2,10 +2,11 @@ package com.imed.costaccount.common.shiro;
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
+import com.auth0.jwt.exceptions.TokenExpiredException;
|
|
|
+import com.imed.costaccount.common.token.ThreadLocalToken;
|
|
|
import com.imed.costaccount.common.util.ErrorResult;
|
|
|
import com.imed.costaccount.common.token.RedisUtil;
|
|
|
import com.imed.costaccount.common.token.JwtUtil;
|
|
|
-import com.imed.costaccount.common.token.ThreadLocalToken;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.shiro.authc.AuthenticationException;
|
|
|
import org.apache.shiro.authc.AuthenticationToken;
|
|
@@ -31,8 +32,8 @@ public class OAuth2Filter extends AuthenticatingFilter {
|
|
|
@Autowired
|
|
|
private JwtUtil jwtUtil;
|
|
|
|
|
|
- @Autowired
|
|
|
- private ThreadLocalToken local;
|
|
|
+// @Autowired
|
|
|
+// private ThreadLocalToken local;
|
|
|
|
|
|
@Autowired
|
|
|
private RedisUtil redisUtil;
|
|
@@ -88,40 +89,31 @@ public class OAuth2Filter extends AuthenticatingFilter {
|
|
|
resp.setHeader("Access-Control-Allow-Credentials", "true");
|
|
|
resp.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
|
|
|
|
|
|
- local.clear();
|
|
|
+// local.clear();
|
|
|
String token = this.getRequestToken(req);
|
|
|
+ // 如果前端传过来的token是null
|
|
|
if (StrUtil.isBlank(token)) {
|
|
|
String json = JSONUtil.toJsonStr(ErrorResult.errorMsg(499, "请先登录"));
|
|
|
resp.getWriter().print(json);
|
|
|
return false;
|
|
|
}
|
|
|
- // 判断redis中是否存在该用户的token,如果不存在或者不一致那么标识token无效
|
|
|
- int userId = jwtUtil.getUserId(token);
|
|
|
- String str = (String) redisUtil.get(userId + "");
|
|
|
- if (StrUtil.isBlank(str)) {
|
|
|
- String json = JSONUtil.toJsonStr(ErrorResult.errorMsg(499, "登录过期"));
|
|
|
- resp.getWriter().print(json);
|
|
|
- return false;
|
|
|
- }
|
|
|
- if (!str.equalsIgnoreCase(token)) {
|
|
|
- String json = JSONUtil.toJsonStr(ErrorResult.errorMsg(499, "令牌无效,请重新登录"));
|
|
|
- resp.getWriter().print(json);
|
|
|
- return false;
|
|
|
- }
|
|
|
|
|
|
// 内容是否过期
|
|
|
try {
|
|
|
jwtUtil.verifierToken(token);
|
|
|
+ } catch (TokenExpiredException e) {
|
|
|
+ resp.setStatus(499);
|
|
|
+ resp.getWriter().print("token过期");
|
|
|
+ String json = JSONUtil.toJsonStr(ErrorResult.errorMsg(499, "令牌过期,请重新登录"));
|
|
|
+ resp.getWriter().print(json);
|
|
|
+ return false;
|
|
|
} catch (Exception e) {
|
|
|
-// // 无效的令牌
|
|
|
- resp.setStatus(400);
|
|
|
+ resp.setStatus(499);
|
|
|
resp.getWriter().print("无效的令牌");
|
|
|
- String json = JSONUtil.toJsonStr(ErrorResult.errorMsg(499, "令牌过期,请重新登录"));
|
|
|
+ String json = JSONUtil.toJsonStr(ErrorResult.errorMsg(499, "令牌异常,请重新登录"));
|
|
|
resp.getWriter().print(json);
|
|
|
return false;
|
|
|
}
|
|
|
- // 执行realm
|
|
|
- local.setToken(token);
|
|
|
return executeLogin(request, response);
|
|
|
}
|
|
|
|