fix: 修复未知异常被 token 无效处理器拦截报错token无效的错误

This commit is contained in:
hxr 2024-03-05 00:23:06 +08:00
parent fcf0c3fdc5
commit 7be21be8d6
2 changed files with 9 additions and 16 deletions

View File

@ -15,7 +15,7 @@ import java.io.IOException;
* 自定义 token 无效异常 * 自定义 token 无效异常
* *
* @author haoxr * @author haoxr
* @date 2022/11/13 * @since 2022/11/13
*/ */
@Component @Component
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint { public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
@ -23,16 +23,8 @@ public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
throws IOException { throws IOException {
response.setContentType("application/json"); response.setContentType("application/json");
int status = response.getStatus();
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
if (HttpServletResponse.SC_NOT_FOUND == status) { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setStatus(HttpServletResponse.SC_NOT_FOUND); mapper.writeValue(response.getOutputStream(), Result.failed(ResultCode.TOKEN_INVALID));
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));
}
} }
} }

View File

@ -23,6 +23,7 @@ import org.springframework.web.servlet.NoHandlerFoundException;
import jakarta.servlet.ServletException; import jakarta.servlet.ServletException;
import jakarta.validation.ConstraintViolation; import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException; import jakarta.validation.ConstraintViolationException;
import java.sql.SQLSyntaxErrorException; import java.sql.SQLSyntaxErrorException;
import java.util.concurrent.CompletionException; import java.util.concurrent.CompletionException;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -201,15 +202,15 @@ public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public <T> Result<T> handleException(Exception e) { public <T> Result<T> handleException(Exception e) {
e.printStackTrace(); log.error("unknown exception:{}", e.getMessage(), e);
String errorMsg = e.getMessage(); String errorMsg = e.getMessage();
if (StrUtil.isNotBlank(errorMsg) && errorMsg.contains("denied to user")) { if (StrUtil.isNotBlank(errorMsg) && errorMsg.contains("denied to user")) {
return Result.failed(ResultCode.FORBIDDEN_OPERATION); 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);
} }
/** /**