|
@@ -40,81 +40,83 @@ public class JWTInterceptor implements HandlerInterceptor {
|
|
|
@Override
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
|
|
throws Exception {
|
|
|
- if (CorsUtils.isPreFlightRequest(request)) {
|
|
|
- // 这是一个OPTIONS请求,我们可以选择放行
|
|
|
- return true;
|
|
|
- }
|
|
|
- //获取请求头中的token
|
|
|
- String token = request.getHeader("Authorization");
|
|
|
- log.info("====================>token值: " + token);
|
|
|
- String path = request.getRequestURI();
|
|
|
- log.info("====================>path: " + path);
|
|
|
- Map<String, Object> map = new HashMap<>();
|
|
|
- int errorType = 0;
|
|
|
- try {
|
|
|
- JWTUtils.TokenVerify(token);
|
|
|
- DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
|
|
|
- log.info("phone:{}", tokenInfo.getClaim("phone").asString());
|
|
|
- String phone = tokenInfo.getClaim("phone").asString();
|
|
|
- log.info("userType:{}", tokenInfo.getClaim("userType").asString());
|
|
|
- String deviceType = tokenInfo.getClaim("userType").asString();
|
|
|
- String redisKey;
|
|
|
- //区分
|
|
|
- if ("web".equals(deviceType)) {
|
|
|
- //管理端单设备登录
|
|
|
-// redisKey = deviceType + "_" + tokenInfo.getClaim("userName").asString();
|
|
|
- //不限制
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- redisKey = deviceType + "_" + tokenInfo.getClaim("phone").asString();
|
|
|
- }
|
|
|
- String redisVal = baseRedisService.getString(redisKey);
|
|
|
- if (StringUtils.isEmpty(redisVal) || !token.equals(redisVal)) {
|
|
|
- //判断程序是否为用户禁用
|
|
|
- StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getPhone, phone));
|
|
|
- if (storeUser.getStatus() == 1) {
|
|
|
- map.put("msg", "你的账号已被禁用");
|
|
|
- //别问, 问就是约定俗成
|
|
|
- map.put("code", 777);
|
|
|
- } else {
|
|
|
- map.put("msg", "用户在别处登录");
|
|
|
- //别问, 问就是约定俗成
|
|
|
- map.put("code", 666);
|
|
|
- }
|
|
|
+// if (CorsUtils.isPreFlightRequest(request)) {
|
|
|
+// // 这是一个OPTIONS请求,我们可以选择放行
|
|
|
+// return true;
|
|
|
+// }
|
|
|
+// //获取请求头中的token
|
|
|
+// String token = request.getHeader("Authorization");
|
|
|
+// log.info("====================>token值: " + token);
|
|
|
+// String path = request.getRequestURI();
|
|
|
+// log.info("====================>path: " + path);
|
|
|
+// Map<String, Object> map = new HashMap<>();
|
|
|
+// int errorType = 0;
|
|
|
+// try {
|
|
|
+// JWTUtils.TokenVerify(token);
|
|
|
+// DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
|
|
|
+// log.info("phone:{}", tokenInfo.getClaim("phone").asString());
|
|
|
+// String phone = tokenInfo.getClaim("phone").asString();
|
|
|
+// log.info("userType:{}", tokenInfo.getClaim("userType").asString());
|
|
|
+// String deviceType = tokenInfo.getClaim("userType").asString();
|
|
|
+// String redisKey;
|
|
|
+// //区分
|
|
|
+// if ("web".equals(deviceType)) {
|
|
|
+// //管理端单设备登录
|
|
|
+//// redisKey = deviceType + "_" + tokenInfo.getClaim("userName").asString();
|
|
|
+// //不限制
|
|
|
+// return true;
|
|
|
+// } else {
|
|
|
+// redisKey = deviceType + "_" + tokenInfo.getClaim("phone").asString();
|
|
|
+// }
|
|
|
+// String redisVal = baseRedisService.getString(redisKey);
|
|
|
+// if (StringUtils.isEmpty(redisVal) || !token.equals(redisVal)) {
|
|
|
+// //判断程序是否为用户禁用
|
|
|
+// StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getPhone, phone));
|
|
|
+// if (storeUser.getStatus() == 1) {
|
|
|
+// map.put("msg", "你的账号已被禁用");
|
|
|
+// //别问, 问就是约定俗成
|
|
|
+// map.put("code", 777);
|
|
|
+// } else {
|
|
|
+// map.put("msg", "用户在别处登录");
|
|
|
+// //别问, 问就是约定俗成
|
|
|
+// map.put("code", 666);
|
|
|
+// }
|
|
|
+//
|
|
|
+// map.put("success", false);
|
|
|
+// String json = new ObjectMapper().writeValueAsString(map);
|
|
|
+// response.setContentType("application/json;charset=UTF-8");
|
|
|
+// response.getWriter().print(json);
|
|
|
+// return false;
|
|
|
+// }
|
|
|
+// //放行请求
|
|
|
+// return true;
|
|
|
+// } catch (SignatureVerificationException e) {
|
|
|
+// errorType = 1;
|
|
|
+// log.error("JWTInterceptor SignatureVerificationException Msg={}", e.getMessage());
|
|
|
+// map.put("msg", "无效签名");
|
|
|
+// } catch (TokenExpiredException e) {
|
|
|
+// errorType = 2;
|
|
|
+// log.error("JWTInterceptor TokenExpiredException Msg={}", e.getMessage());
|
|
|
+// map.put("msg", "token已过期");
|
|
|
+// } catch (AlgorithmMismatchException e) {
|
|
|
+// errorType = 3;
|
|
|
+// log.error("JWTInterceptor AlgorithmMismatchException Msg={}", e.getMessage());
|
|
|
+// map.put("msg", "算法不一致");
|
|
|
+// } catch (Exception e) {
|
|
|
+// errorType = 4;
|
|
|
+// log.error("JWTInterceptor Exception Msg={}", e.getMessage());
|
|
|
+// map.put("msg", "token无效");
|
|
|
+// }
|
|
|
+// log.info("====================>token无效类型: " + errorType);
|
|
|
+// map.put("code", 401);
|
|
|
+// map.put("success", false);
|
|
|
+// //使用jackson将map转为json
|
|
|
+// String json = new ObjectMapper().writeValueAsString(map);
|
|
|
+// response.setContentType("application/json;charset=UTF-8");
|
|
|
+// response.getWriter().print(json);
|
|
|
+// return false;
|
|
|
|
|
|
- map.put("success", false);
|
|
|
- String json = new ObjectMapper().writeValueAsString(map);
|
|
|
- response.setContentType("application/json;charset=UTF-8");
|
|
|
- response.getWriter().print(json);
|
|
|
- return false;
|
|
|
- }
|
|
|
- //放行请求
|
|
|
- return true;
|
|
|
- } catch (SignatureVerificationException e) {
|
|
|
- errorType = 1;
|
|
|
- log.error("JWTInterceptor SignatureVerificationException Msg={}", e.getMessage());
|
|
|
- map.put("msg", "无效签名");
|
|
|
- } catch (TokenExpiredException e) {
|
|
|
- errorType = 2;
|
|
|
- log.error("JWTInterceptor TokenExpiredException Msg={}", e.getMessage());
|
|
|
- map.put("msg", "token已过期");
|
|
|
- } catch (AlgorithmMismatchException e) {
|
|
|
- errorType = 3;
|
|
|
- log.error("JWTInterceptor AlgorithmMismatchException Msg={}", e.getMessage());
|
|
|
- map.put("msg", "算法不一致");
|
|
|
- } catch (Exception e) {
|
|
|
- errorType = 4;
|
|
|
- log.error("JWTInterceptor Exception Msg={}", e.getMessage());
|
|
|
- map.put("msg", "token无效");
|
|
|
- }
|
|
|
- log.info("====================>token无效类型: " + errorType);
|
|
|
- map.put("code", 401);
|
|
|
- map.put("success", false);
|
|
|
- //使用jackson将map转为json
|
|
|
- String json = new ObjectMapper().writeValueAsString(map);
|
|
|
- response.setContentType("application/json;charset=UTF-8");
|
|
|
- response.getWriter().print(json);
|
|
|
- return false;
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
}
|