同步代码

This commit is contained in:
lulz1 2024-02-27 15:20:05 +08:00
parent 8083483fa9
commit 0858c77f87
6 changed files with 34 additions and 7 deletions

View File

@ -0,0 +1,7 @@
package com.chint.domain.exceptions;
public class JwtExpiredException extends BaseException{
public JwtExpiredException(String msg) {
super(msg);
}
}

View File

@ -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";
}

View File

@ -39,9 +39,13 @@ public class GlobalExceptionHandler {
}
@ExceptionHandler(AuthException.class)
public Result<String> handleDuplicateException(HttpServletRequest request, AuthException e) {
public Result<String> handleAuthException(HttpServletRequest request, AuthException e) {
return Result.error(e.getMessage());
}
@ExceptionHandler(JwtExpiredException.class)
public Result<String> handleJwtExpiredException(HttpServletRequest request, JwtExpiredException e) {
return Result.tokenExpired(e.getMessage());
}
@ExceptionHandler(value = Exception.class)
public Result<String> handleException(Exception ex) {

View File

@ -49,7 +49,9 @@ public class Result<T> implements Serializable {
public static <T> Result<T> error(String msg) {
return new Result<T>(msg, "0");
}
public static <T> Result<T> tokenExpired(String msg) {
return new Result<T>(msg, "-1");
}
@Override
public boolean equals(Object o) {

View File

@ -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);
}
}

View File

@ -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);
}
}