feat(security): 添加自定义认证和授权异常处理器

- 新增 JwtAccessDeniedHandler 处理权限不足异常
- 新增 JwtAuthenticationEntryPoint 处理认证失败异常- 在 SecurityConfig 中集成自定义异常处理器
- 优化 GlobalExceptionHandler 中的异常日志输出
This commit is contained in:
2025-08-01 08:58:41 +08:00
parent 950955800d
commit 7a7247a851
4 changed files with 97 additions and 7 deletions

View File

@@ -69,11 +69,20 @@ public class GlobalExceptionHandler {
}
/**
* 处理其他异常
* 处理请求体格式错误异常 (例如JSON格式错误)
*/
@ExceptionHandler(org.springframework.http.converter.HttpMessageNotReadableException.class)
public R<String> handleHttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException e) {
log.error("请求参数格式不正确: {}", e.getMessage());
return R.fail(ResultCode.VALIDATE_FAILED.getCode(), "请求参数格式不正确");
}
/**
* 处理其他所有未捕获的异常
*/
@ExceptionHandler(Exception.class)
public R<Void> handleException(Exception e) {
log.error("系统异常:{}", e.getMessage());
return R.fail(ResultCode.FAILED.getCode(), "系统繁忙,请稍后再试" + e.getMessage());
log.error("系统异常:", e); // 打印完整的堆栈信息
return R.fail(ResultCode.FAILED.getCode(), "系统繁忙,请稍后再试");
}
}