diff --git a/youlai-common/common-security/src/main/java/com/youlai/common/security/exception/MyAuthenticationEntryPoint.java b/youlai-common/common-security/src/main/java/com/youlai/common/security/exception/MyAuthenticationEntryPoint.java index e0cb54cfd220c4c3855785443b5a1652bbb38f3b..3f3a9b0408bb6d9b1f49907d2e0c280e5ddfc9ae 100644 --- a/youlai-common/common-security/src/main/java/com/youlai/common/security/exception/MyAuthenticationEntryPoint.java +++ b/youlai-common/common-security/src/main/java/com/youlai/common/security/exception/MyAuthenticationEntryPoint.java @@ -15,7 +15,7 @@ import java.io.IOException; * 自定义 token 无效异常 * * @author haoxr - * @date 2022/11/13 + * @since 2022/11/13 */ @Component public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint { @@ -23,16 +23,8 @@ public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint { public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException { response.setContentType("application/json"); - - int status = response.getStatus(); ObjectMapper mapper = new ObjectMapper(); - if (HttpServletResponse.SC_NOT_FOUND == status) { - response.setStatus(HttpServletResponse.SC_NOT_FOUND); - mapper.writeValue(response.getOutputStream(), Result.failed(ResultCode.RESOURCE_NOT_FOUND)); - } else { - response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); - mapper.writeValue(response.getOutputStream(), Result.failed(ResultCode.TOKEN_INVALID)); - } - + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + mapper.writeValue(response.getOutputStream(), Result.failed(ResultCode.TOKEN_INVALID)); } } diff --git a/youlai-common/common-web/src/main/java/com/youlai/common/web/exception/GlobalExceptionHandler.java b/youlai-common/common-web/src/main/java/com/youlai/common/web/exception/GlobalExceptionHandler.java index 934b1884cdb525bbe06889f598895f5fee98c2b2..c62c81bc8e8ed59c391274469a82294923e04cba 100644 --- a/youlai-common/common-web/src/main/java/com/youlai/common/web/exception/GlobalExceptionHandler.java +++ b/youlai-common/common-web/src/main/java/com/youlai/common/web/exception/GlobalExceptionHandler.java @@ -23,6 +23,7 @@ import org.springframework.web.servlet.NoHandlerFoundException; import jakarta.servlet.ServletException; import jakarta.validation.ConstraintViolation; import jakarta.validation.ConstraintViolationException; + import java.sql.SQLSyntaxErrorException; import java.util.concurrent.CompletionException; import java.util.regex.Matcher; @@ -201,15 +202,15 @@ public class GlobalExceptionHandler { @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(Exception.class) public Result handleException(Exception e) { - e.printStackTrace(); + log.error("unknown exception:{}", e.getMessage(), e); String errorMsg = e.getMessage(); if (StrUtil.isNotBlank(errorMsg) && errorMsg.contains("denied to user")) { return Result.failed(ResultCode.FORBIDDEN_OPERATION); - }else{ - log.error("unknown exception"); - errorMsg=e.getCause().getMessage(); - return Result.failed(errorMsg); } + if (StrUtil.isBlank(errorMsg)) { + errorMsg = "系统异常"; + } + return Result.failed(errorMsg); } /**