diff --git a/dinky-admin/src/main/java/org/dinky/aop/WebExceptionHandler.java b/dinky-admin/src/main/java/org/dinky/aop/WebExceptionHandler.java index e21a96a2f1..8e9d7a703b 100644 --- a/dinky-admin/src/main/java/org/dinky/aop/WebExceptionHandler.java +++ b/dinky-admin/src/main/java/org/dinky/aop/WebExceptionHandler.java @@ -28,6 +28,7 @@ import org.apache.commons.lang3.StringUtils; import java.util.List; +import java.util.Map; import javax.servlet.http.HttpServletResponse; @@ -46,6 +47,7 @@ import org.springframework.web.context.request.ServletRequestAttributes; import cn.dev33.satoken.exception.NotLoginException; +import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.StrUtil; /** @@ -67,6 +69,17 @@ public Result busException(BusException e) { return Result.failed(e.getMsg()); } + private static final Map ERR_CODE_MAPPING = + MapUtil.builder() + .put(NotLoginException.NOT_TOKEN, Status.NOT_TOKEN) + .put(NotLoginException.INVALID_TOKEN, Status.INVALID_TOKEN) + .put(NotLoginException.TOKEN_TIMEOUT, Status.EXPIRED_TOKEN) + .put(NotLoginException.BE_REPLACED, Status.BE_REPLACED) + .put(NotLoginException.KICK_OUT, Status.KICK_OUT) + .put(NotLoginException.TOKEN_FREEZE, Status.TOKEN_FREEZED) + .put(NotLoginException.NO_PREFIX, Status.NO_PREFIX) + .build(); + @ExceptionHandler public Result notLoginException(NotLoginException notLoginException) { ServletRequestAttributes servletRequestAttributes = @@ -75,23 +88,10 @@ public Result notLoginException(NotLoginException notLoginException) { if (response != null) { response.setStatus(CodeEnum.NOTLOGIN.getCode()); } - if (notLoginException.getType().equals(NotLoginException.NOT_TOKEN)) { - return Result.failed(Status.NOT_TOKEN); - } else if (notLoginException.getType().equals(NotLoginException.INVALID_TOKEN)) { - return Result.failed(Status.INVALID_TOKEN); - } else if (notLoginException.getType().equals(NotLoginException.TOKEN_TIMEOUT)) { - return Result.failed(Status.EXPIRED_TOKEN); - } else if (notLoginException.getType().equals(NotLoginException.BE_REPLACED)) { - return Result.failed(Status.BE_REPLACED); - } else if (notLoginException.getType().equals(NotLoginException.KICK_OUT)) { - return Result.failed(Status.KICK_OUT); - } else if (notLoginException.getType().equals(NotLoginException.TOKEN_FREEZE)) { - return Result.failed(Status.TOKEN_FREEZED); - } else if (notLoginException.getType().equals(NotLoginException.NO_PREFIX)) { - return Result.failed(Status.NO_PREFIX); - } else { - return Result.failed(Status.USER_NOT_LOGIN); - } + + String type = notLoginException.getType(); + Status status = ERR_CODE_MAPPING.getOrDefault(type, Status.NOT_TOKEN); + return Result.failed(status); } /**