diff --git a/src/main/java/com/chint/domain/exceptions/JwtExpiredException.java b/src/main/java/com/chint/domain/exceptions/JwtExpiredException.java new file mode 100644 index 00000000..d1402b01 --- /dev/null +++ b/src/main/java/com/chint/domain/exceptions/JwtExpiredException.java @@ -0,0 +1,7 @@ +package com.chint.domain.exceptions; + +public class JwtExpiredException extends BaseException{ + public JwtExpiredException(String msg) { + super(msg); + } +} diff --git a/src/main/java/com/chint/infrastructure/constant/AuthMessageConstant.java b/src/main/java/com/chint/infrastructure/constant/AuthMessageConstant.java index 170d59cb..dc5e0a50 100644 --- a/src/main/java/com/chint/infrastructure/constant/AuthMessageConstant.java +++ b/src/main/java/com/chint/infrastructure/constant/AuthMessageConstant.java @@ -34,4 +34,5 @@ public class AuthMessageConstant { public static final String SUBJECT = "EmployeeNo"; public static final long EXPIRATION_TIME_MS = 3600000L; // 1小时过期时间 public static final String HEADER_TOKEN = "token"; + public static final String HEADER_TOKEN_UP_CASE = "Token"; } diff --git a/src/main/java/com/chint/infrastructure/handler/GlobalExceptionHandler.java b/src/main/java/com/chint/infrastructure/handler/GlobalExceptionHandler.java index a933ac3c..edddf647 100644 --- a/src/main/java/com/chint/infrastructure/handler/GlobalExceptionHandler.java +++ b/src/main/java/com/chint/infrastructure/handler/GlobalExceptionHandler.java @@ -39,9 +39,13 @@ public class GlobalExceptionHandler { } @ExceptionHandler(AuthException.class) - public Result handleDuplicateException(HttpServletRequest request, AuthException e) { + public Result handleAuthException(HttpServletRequest request, AuthException e) { return Result.error(e.getMessage()); } + @ExceptionHandler(JwtExpiredException.class) + public Result handleJwtExpiredException(HttpServletRequest request, JwtExpiredException e) { + return Result.tokenExpired(e.getMessage()); + } @ExceptionHandler(value = Exception.class) public Result handleException(Exception ex) { diff --git a/src/main/java/com/chint/infrastructure/util/Result.java b/src/main/java/com/chint/infrastructure/util/Result.java index 15367c8c..9117f680 100644 --- a/src/main/java/com/chint/infrastructure/util/Result.java +++ b/src/main/java/com/chint/infrastructure/util/Result.java @@ -49,7 +49,9 @@ public class Result implements Serializable { public static Result error(String msg) { return new Result(msg, "0"); } - + public static Result tokenExpired(String msg) { + return new Result(msg, "-1"); + } @Override public boolean equals(Object o) { diff --git a/src/main/java/com/chint/infrastructure/webconfig/JwtTokenAdminInterceptor.java b/src/main/java/com/chint/infrastructure/webconfig/JwtTokenAdminInterceptor.java index f8dee193..b2ce91aa 100644 --- a/src/main/java/com/chint/infrastructure/webconfig/JwtTokenAdminInterceptor.java +++ b/src/main/java/com/chint/infrastructure/webconfig/JwtTokenAdminInterceptor.java @@ -3,7 +3,7 @@ package com.chint.infrastructure.webconfig; import com.auth0.jwt.exceptions.TokenExpiredException; import com.chint.domain.aggregates.user.User; import com.chint.domain.exceptions.AuthException; -import com.chint.domain.exceptions.CommandException; +import com.chint.domain.exceptions.JwtExpiredException; import com.chint.infrastructure.constant.AuthMessageConstant; import com.chint.infrastructure.util.BaseContext; import com.chint.infrastructure.util.JWTUtil; @@ -18,6 +18,9 @@ public class JwtTokenAdminInterceptor implements HandlerInterceptor { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String token = request.getHeader(AuthMessageConstant.HEADER_TOKEN); + if (token == null) { + token = request.getHeader(AuthMessageConstant.HEADER_TOKEN_UP_CASE); + } if (request.getRequestURI().contains("/pubilc")) { return true; } @@ -32,10 +35,9 @@ public class JwtTokenAdminInterceptor implements HandlerInterceptor { var withJwt = User.withJwt(parseJWT); BaseContext.setCurrentUser(withJwt); return true; // If verification succeeds, continue processing the request - }catch (TokenExpiredException e) { - throw new AuthException(AuthMessageConstant.JWT_EXPIRED); - } - catch (Exception e) { + } catch (TokenExpiredException e) { + throw new JwtExpiredException(AuthMessageConstant.JWT_EXPIRED); + } catch (Exception e) { throw new AuthException(AuthMessageConstant.JWT_INVALID); } } diff --git a/src/test/java/com/chint/RouteApplicationTests.java b/src/test/java/com/chint/RouteApplicationTests.java index c118c8a9..71a0ed47 100644 --- a/src/test/java/com/chint/RouteApplicationTests.java +++ b/src/test/java/com/chint/RouteApplicationTests.java @@ -1,6 +1,7 @@ package com.chint; import cn.hutool.extra.pinyin.PinyinUtil; +import com.chint.application.services.login.strategy.PailaLoginStrategy; import com.chint.domain.aggregates.order.Location; import com.chint.domain.aggregates.user.User; import com.chint.domain.repository.LocationRepository; @@ -33,6 +34,10 @@ class RouteApplicationTests { @Autowired private OrderDetailRepository orderDetailRepository; + + @Autowired + private PailaLoginStrategy pailaLoginStrategy; + private User user = new User(1L, 230615020L, 1, "卢麟哲", "1033719135@qq.com", "15857193365"); @@ -113,4 +118,10 @@ class RouteApplicationTests { orderDetailRepository.deleteById(31L); orderDetailRepository.deleteById(33L); } + + @Test + void ssoLogin(){ + User login = pailaLoginStrategy.login("OC-5909-zRqrWjZGNThNXJiAV1kA7dPXTojGzVxK3nE"); + System.out.println(login); + } }