提交 7be21be8 编写于 作者: H hxr

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

上级 fcf0c3fd
......@@ -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));
}
}
......@@ -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 <T> Result<T> 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);
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册