|
@@ -1,120 +1,120 @@
|
|
|
-package shop.alien.gateway.config;
|
|
|
-
|
|
|
-import com.auth0.jwt.exceptions.AlgorithmMismatchException;
|
|
|
-import com.auth0.jwt.exceptions.SignatureVerificationException;
|
|
|
-import com.auth0.jwt.exceptions.TokenExpiredException;
|
|
|
-import com.auth0.jwt.interfaces.DecodedJWT;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.web.cors.CorsUtils;
|
|
|
-import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
-import shop.alien.entity.store.StoreUser;
|
|
|
-import shop.alien.gateway.mapper.StoreUserMapper;
|
|
|
-
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-/**
|
|
|
- * Token校验
|
|
|
- *
|
|
|
- * @author ssk
|
|
|
- * @version 1.0
|
|
|
- * @date 2024/12/25 16:59
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-@Component
|
|
|
-public class JWTInterceptor implements HandlerInterceptor {
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private BaseRedisService baseRedisService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private StoreUserMapper storeUserMapper;
|
|
|
-
|
|
|
- @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);
|
|
|
- }
|
|
|
-
|
|
|
- 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;
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+//package shop.alien.gateway.config;
|
|
|
+//
|
|
|
+//import com.auth0.jwt.exceptions.AlgorithmMismatchException;
|
|
|
+//import com.auth0.jwt.exceptions.SignatureVerificationException;
|
|
|
+//import com.auth0.jwt.exceptions.TokenExpiredException;
|
|
|
+//import com.auth0.jwt.interfaces.DecodedJWT;
|
|
|
+//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+//import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
+//import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+//import lombok.extern.slf4j.Slf4j;
|
|
|
+//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+//import org.springframework.stereotype.Component;
|
|
|
+//import org.springframework.web.cors.CorsUtils;
|
|
|
+//import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
+//import shop.alien.entity.store.StoreUser;
|
|
|
+//import shop.alien.gateway.mapper.StoreUserMapper;
|
|
|
+//
|
|
|
+//import javax.servlet.http.HttpServletRequest;
|
|
|
+//import javax.servlet.http.HttpServletResponse;
|
|
|
+//import java.util.HashMap;
|
|
|
+//import java.util.Map;
|
|
|
+//
|
|
|
+///**
|
|
|
+// * Token校验
|
|
|
+// *
|
|
|
+// * @author ssk
|
|
|
+// * @version 1.0
|
|
|
+// * @date 2024/12/25 16:59
|
|
|
+// */
|
|
|
+//@Slf4j
|
|
|
+//@Component
|
|
|
+//public class JWTInterceptor implements HandlerInterceptor {
|
|
|
+//
|
|
|
+// @Autowired
|
|
|
+// private BaseRedisService baseRedisService;
|
|
|
+//
|
|
|
+// @Autowired
|
|
|
+// private StoreUserMapper storeUserMapper;
|
|
|
+//
|
|
|
+// @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);
|
|
|
+// }
|
|
|
+//
|
|
|
+// 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;
|
|
|
+// }
|
|
|
+//
|
|
|
+//}
|