From 8d30ca7eee65e3748449f0abfd25682c54d5dc92 Mon Sep 17 00:00:00 2001 From: zhaojun <873019219@qq.com> Date: Mon, 29 Aug 2022 14:44:12 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=E9=80=9A=E8=BF=87=20?= =?UTF-8?q?nginx=20=E5=8F=8D=E5=90=91=E4=BB=A3=E7=90=86=E5=90=8E,=20?= =?UTF-8?q?=E6=9C=AA=E7=99=BB=E5=BD=95=E5=90=8E=E5=8F=B0=E6=97=B6=E8=AF=B7?= =?UTF-8?q?=E6=B1=82,=20=E9=87=8D=E5=AE=9A=E5=90=91=E5=88=B0=E5=8F=8D?= =?UTF-8?q?=E4=BB=A3=E5=89=8D=E5=9C=B0=E5=9D=80=E7=9A=84=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/im/zhaojun/zfile/common/exception/GlobalException.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/im/zhaojun/zfile/common/exception/GlobalException.java b/src/main/java/im/zhaojun/zfile/common/exception/GlobalException.java index bba9bdb..1cb5f1c 100644 --- a/src/main/java/im/zhaojun/zfile/common/exception/GlobalException.java +++ b/src/main/java/im/zhaojun/zfile/common/exception/GlobalException.java @@ -1 +1 @@ -package im.zhaojun.zfile.common.exception; import cn.dev33.satoken.exception.NotLoginException; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import im.zhaojun.zfile.admin.exception.ForbidFileOperationException; import im.zhaojun.zfile.admin.exception.StorageSourceAutoConfigCorsException; import im.zhaojun.zfile.common.exception.file.operator.GetFileInfoException; import im.zhaojun.zfile.common.exception.file.operator.ProxyDownloadFileException; import im.zhaojun.zfile.common.util.AjaxJson; import im.zhaojun.zfile.common.util.RequestHolder; import lombok.extern.slf4j.Slf4j; import org.apache.catalina.connector.ClientAbortException; import org.springframework.http.HttpStatus; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.validation.BindException; import org.springframework.validation.BindingResult; import org.springframework.web.HttpMediaTypeNotAcceptableException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * 全局异常处理 * * @author zhaojun */ @ControllerAdvice @Slf4j public class GlobalException { @ExceptionHandler({IllegalDownloadLinkException.class}) @ResponseBody @ResponseStatus(HttpStatus.FORBIDDEN) public AjaxJson getFileInfoException(IllegalDownloadLinkException e) { return AjaxJson.getError(e.getMessage()); } @ExceptionHandler({GetFileInfoException.class}) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public AjaxJson getFileInfoException(GetFileInfoException e) { log.error("获取文件信息异常 {}. 存储源 ID: {}, 文件路径: {}", e.getMessage(), e.getStorageId(), e.getPathAndName()); return AjaxJson.getError("获取文件信息异常, 请联系管理员"); } @ExceptionHandler({ProxyDownloadFileException.class}) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public AjaxJson proxyDownloadFileException(ProxyDownloadFileException e) { log.error("代理下载文件异常 {}. 存储源 ID: {}, 下载路径: {}", e.getMessage(), e.getStorageId(), e.getPathAndName()); return AjaxJson.getError("下载文件异常, 请联系管理员!"); } @ExceptionHandler({LoginVerifyException.class}) @ResponseBody @ResponseStatus(HttpStatus.FORBIDDEN) public AjaxJson loginVerifyException(LoginVerifyException e) { return AjaxJson.getError(e.getMessage()); } @ExceptionHandler({StorageSourceNotSupportProxyUploadException.class}) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public AjaxJson storageSourceNotSupportProxyUploadException(StorageSourceNotSupportProxyUploadException e) { return AjaxJson.getError("非法操作, 当前存储源不支持此方式上传."); } @ExceptionHandler({DisableProxyDownloadException.class}) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public AjaxJson disableProxyDownloadException(DisableProxyDownloadException e) { return AjaxJson.getError("非法操作, 当前文件不支持此类下载方式."); } @ExceptionHandler({UnsupportedOperationException.class}) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public AjaxJson handleFileUploadException(UnsupportedOperationException e) { return AjaxJson.getError(e.getMessage()); } @ExceptionHandler({FileUploadException.class}) @ResponseBody @ResponseStatus(HttpStatus.UNAUTHORIZED) public AjaxJson handleFileUploadException(FileUploadException e) { if (log.isDebugEnabled()) { log.debug("上传文件失败: 存储类型: {}, 存储源 id: {}, 存储源路径: {}", e.getStorageTypeEnum(), e.getStorageId(), e.getPath(), e); } return AjaxJson.getError("上传失败"); } @ExceptionHandler({FileAccessException.class}) @ResponseBody @ResponseStatus(HttpStatus.UNAUTHORIZED) public AjaxJson handleFileAccessException(FileAccessException e) { return AjaxJson.getError(e.getMessage()); } @ExceptionHandler({HttpMessageNotReadableException.class}) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public AjaxJson handlerHttpMessageNotReadableException(HttpMessageNotReadableException e) { return AjaxJson.getBadRequestError("请求参数不合法"); } @ExceptionHandler({NotEnabledStorageSourceException.class}) @ResponseStatus(HttpStatus.FORBIDDEN) @ResponseBody public AjaxJson notEnabledDrive() { return AjaxJson.getError("存储源已关闭"); } @ExceptionHandler({NotExistFileException.class}) @ResponseBody public AjaxJson notExistFile() { return AjaxJson.getError("文件不存在"); } /** * 捕获 ClientAbortException 异常, 不做任何处理, 防止出现大量堆栈日志输出, 此异常不影响功能. */ @ExceptionHandler({HttpMediaTypeNotAcceptableException.class, ClientAbortException.class}) @ResponseBody @ResponseStatus public void clientAbortException() { // if (log.isDebugEnabled()) { // log.debug("出现了断开异常:", ex); // } } @ExceptionHandler(value = {MethodArgumentNotValidException.class, BindException.class}) @ResponseBody public AjaxJson handleValidException(Exception e) { BindingResult bindingResult = null; if (e instanceof MethodArgumentNotValidException) { bindingResult = ((MethodArgumentNotValidException) e).getBindingResult(); } else if (e instanceof BindException) { bindingResult = ((BindException) e).getBindingResult(); } Map errorMap = new HashMap<>(16); bindingResult.getFieldErrors().forEach((fieldError) -> errorMap.put(fieldError.getField(), fieldError.getDefaultMessage()) ); return new AjaxJson(400, "非法参数 !", errorMap); } /** * 密码校验异常 */ @ExceptionHandler({PasswordVerifyException.class}) @ResponseBody @ResponseStatus public AjaxJson passwordVerifyException(PasswordVerifyException ex) { return AjaxJson.get(ex.getCode(), ex.getMessage()); } /** * 无效的存储源异常 */ @ExceptionHandler({InvalidStorageSourceException.class}) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public AjaxJson invalidDriveException() { return AjaxJson.getBadRequestError("此存储源不存在或初始化失败, 请检查后台参数配置"); } /** * 文件预览异常 */ @ExceptionHandler({PreviewException.class}) @ResponseBody @ResponseStatus public AjaxJson previewException(PreviewException ex) { return AjaxJson.getError(ex.getMessage()); } /** * 初始化异常 */ @ExceptionHandler({InitializeStorageSourceException.class}) @ResponseBody @ResponseStatus public AjaxJson initializeException(InitializeStorageSourceException ex) { return AjaxJson.getError(ex.getMessage()); } /** * 初始化时自动设置 cors 异常 */ @ExceptionHandler({StorageSourceAutoConfigCorsException.class}) @ResponseBody @ResponseStatus public AjaxJson autoConfigCorsException(StorageSourceAutoConfigCorsException ex) { log.error("跨域配置失败, 存储源初始化信息 {}, 异常信息: ", JSON.toJSON(ex.getIStorageParam()), ex); return AjaxJson.getError(ex.getMessage()); } /** * 无权限时,进行文件操作异常. */ @ExceptionHandler({ForbidFileOperationException.class}) @ResponseBody @ResponseStatus public AjaxJson autoConfigCorsException(ForbidFileOperationException ex) { log.error("存储源 {} 拦截非法的文件操作 {}", ex.getStorageId(), ex.getAction()); return AjaxJson.getError("非法操作"); } @ExceptionHandler @ResponseBody @ResponseStatus public AjaxJson extraExceptionHandler(Exception e) { log.error(e.getMessage(), e); if (e.getClass() == Exception.class) { return AjaxJson.getError("系统异常, 请联系管理员"); } else { return AjaxJson.getError(e.getMessage()); } } /** * 登录异常拦截器 */ @ExceptionHandler(NotLoginException.class) @ResponseStatus(HttpStatus.UNAUTHORIZED) @ResponseBody public AjaxJson handlerNotLoginException(NotLoginException e) { HttpServletRequest request = RequestHolder.getRequest(); String axiosRequest = request.getHeader("axios-request"); if (StrUtil.isNotEmpty(axiosRequest)){ return AjaxJson.getNotLogin(); } try { RequestHolder.getResponse().sendRedirect("/login"); } catch (IOException ex) { throw new RuntimeException(ex); } return null; } } \ No newline at end of file +package im.zhaojun.zfile.common.exception; import cn.dev33.satoken.exception.NotLoginException; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import im.zhaojun.zfile.admin.exception.ForbidFileOperationException; import im.zhaojun.zfile.admin.exception.StorageSourceAutoConfigCorsException; import im.zhaojun.zfile.admin.service.SystemConfigService; import im.zhaojun.zfile.common.exception.file.operator.GetFileInfoException; import im.zhaojun.zfile.common.exception.file.operator.ProxyDownloadFileException; import im.zhaojun.zfile.common.util.AjaxJson; import im.zhaojun.zfile.common.util.RequestHolder; import im.zhaojun.zfile.common.util.StringUtils; import lombok.extern.slf4j.Slf4j; import org.apache.catalina.connector.ClientAbortException; import org.springframework.http.HttpStatus; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.validation.BindException; import org.springframework.validation.BindingResult; import org.springframework.web.HttpMediaTypeNotAcceptableException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * 全局异常处理 * * @author zhaojun */ @ControllerAdvice @Slf4j public class GlobalException { @Resource private SystemConfigService systemConfigService; @ExceptionHandler({IllegalDownloadLinkException.class}) @ResponseBody @ResponseStatus(HttpStatus.FORBIDDEN) public AjaxJson getFileInfoException(IllegalDownloadLinkException e) { return AjaxJson.getError(e.getMessage()); } @ExceptionHandler({GetFileInfoException.class}) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public AjaxJson getFileInfoException(GetFileInfoException e) { log.error("获取文件信息异常 {}. 存储源 ID: {}, 文件路径: {}", e.getMessage(), e.getStorageId(), e.getPathAndName()); return AjaxJson.getError("获取文件信息异常, 请联系管理员"); } @ExceptionHandler({ProxyDownloadFileException.class}) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public AjaxJson proxyDownloadFileException(ProxyDownloadFileException e) { log.error("代理下载文件异常 {}. 存储源 ID: {}, 下载路径: {}", e.getMessage(), e.getStorageId(), e.getPathAndName()); return AjaxJson.getError("下载文件异常, 请联系管理员!"); } @ExceptionHandler({LoginVerifyException.class}) @ResponseBody @ResponseStatus(HttpStatus.FORBIDDEN) public AjaxJson loginVerifyException(LoginVerifyException e) { return AjaxJson.getError(e.getMessage()); } @ExceptionHandler({StorageSourceNotSupportProxyUploadException.class}) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public AjaxJson storageSourceNotSupportProxyUploadException(StorageSourceNotSupportProxyUploadException e) { return AjaxJson.getError("非法操作, 当前存储源不支持此方式上传."); } @ExceptionHandler({DisableProxyDownloadException.class}) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public AjaxJson disableProxyDownloadException(DisableProxyDownloadException e) { return AjaxJson.getError("非法操作, 当前文件不支持此类下载方式."); } @ExceptionHandler({UnsupportedOperationException.class}) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public AjaxJson handleFileUploadException(UnsupportedOperationException e) { return AjaxJson.getError(e.getMessage()); } @ExceptionHandler({FileUploadException.class}) @ResponseBody @ResponseStatus(HttpStatus.UNAUTHORIZED) public AjaxJson handleFileUploadException(FileUploadException e) { if (log.isDebugEnabled()) { log.debug("上传文件失败: 存储类型: {}, 存储源 id: {}, 存储源路径: {}", e.getStorageTypeEnum(), e.getStorageId(), e.getPath(), e); } return AjaxJson.getError("上传失败"); } @ExceptionHandler({FileAccessException.class}) @ResponseBody @ResponseStatus(HttpStatus.UNAUTHORIZED) public AjaxJson handleFileAccessException(FileAccessException e) { return AjaxJson.getError(e.getMessage()); } @ExceptionHandler({HttpMessageNotReadableException.class}) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public AjaxJson handlerHttpMessageNotReadableException(HttpMessageNotReadableException e) { return AjaxJson.getBadRequestError("请求参数不合法"); } @ExceptionHandler({NotEnabledStorageSourceException.class}) @ResponseStatus(HttpStatus.FORBIDDEN) @ResponseBody public AjaxJson notEnabledDrive() { return AjaxJson.getError("存储源已关闭"); } @ExceptionHandler({NotExistFileException.class}) @ResponseBody public AjaxJson notExistFile() { return AjaxJson.getError("文件不存在"); } /** * 捕获 ClientAbortException 异常, 不做任何处理, 防止出现大量堆栈日志输出, 此异常不影响功能. */ @ExceptionHandler({HttpMediaTypeNotAcceptableException.class, ClientAbortException.class}) @ResponseBody @ResponseStatus public void clientAbortException() { // if (log.isDebugEnabled()) { // log.debug("出现了断开异常:", ex); // } } @ExceptionHandler(value = {MethodArgumentNotValidException.class, BindException.class}) @ResponseBody public AjaxJson handleValidException(Exception e) { BindingResult bindingResult = null; if (e instanceof MethodArgumentNotValidException) { bindingResult = ((MethodArgumentNotValidException) e).getBindingResult(); } else if (e instanceof BindException) { bindingResult = ((BindException) e).getBindingResult(); } Map errorMap = new HashMap<>(16); bindingResult.getFieldErrors().forEach((fieldError) -> errorMap.put(fieldError.getField(), fieldError.getDefaultMessage()) ); return new AjaxJson(400, "非法参数 !", errorMap); } /** * 密码校验异常 */ @ExceptionHandler({PasswordVerifyException.class}) @ResponseBody @ResponseStatus public AjaxJson passwordVerifyException(PasswordVerifyException ex) { return AjaxJson.get(ex.getCode(), ex.getMessage()); } /** * 无效的存储源异常 */ @ExceptionHandler({InvalidStorageSourceException.class}) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public AjaxJson invalidDriveException() { return AjaxJson.getBadRequestError("此存储源不存在或初始化失败, 请检查后台参数配置"); } /** * 文件预览异常 */ @ExceptionHandler({PreviewException.class}) @ResponseBody @ResponseStatus public AjaxJson previewException(PreviewException ex) { return AjaxJson.getError(ex.getMessage()); } /** * 初始化异常 */ @ExceptionHandler({InitializeStorageSourceException.class}) @ResponseBody @ResponseStatus public AjaxJson initializeException(InitializeStorageSourceException ex) { return AjaxJson.getError(ex.getMessage()); } /** * 初始化时自动设置 cors 异常 */ @ExceptionHandler({StorageSourceAutoConfigCorsException.class}) @ResponseBody @ResponseStatus public AjaxJson autoConfigCorsException(StorageSourceAutoConfigCorsException ex) { log.error("跨域配置失败, 存储源初始化信息 {}, 异常信息: ", JSON.toJSON(ex.getIStorageParam()), ex); return AjaxJson.getError(ex.getMessage()); } /** * 无权限时,进行文件操作异常. */ @ExceptionHandler({ForbidFileOperationException.class}) @ResponseBody @ResponseStatus public AjaxJson autoConfigCorsException(ForbidFileOperationException ex) { log.error("存储源 {} 拦截非法的文件操作 {}", ex.getStorageId(), ex.getAction()); return AjaxJson.getError("非法操作"); } @ExceptionHandler @ResponseBody @ResponseStatus public AjaxJson extraExceptionHandler(Exception e) { log.error(e.getMessage(), e); if (e.getClass() == Exception.class) { return AjaxJson.getError("系统异常, 请联系管理员"); } else { return AjaxJson.getError(e.getMessage()); } } /** * 登录异常拦截器 */ @ExceptionHandler(NotLoginException.class) @ResponseStatus(HttpStatus.UNAUTHORIZED) @ResponseBody public AjaxJson handlerNotLoginException(NotLoginException e) { HttpServletRequest request = RequestHolder.getRequest(); String axiosRequest = request.getHeader("axios-request"); if (StrUtil.isNotEmpty(axiosRequest)){ return AjaxJson.getNotLogin(); } try { String domain = systemConfigService.getRealFrontDomain(); String loginUrl = StringUtils.removeDuplicateSlashes(domain + "/login"); RequestHolder.getResponse().sendRedirect(loginUrl); } catch (IOException ex) { throw new RuntimeException(ex); } return null; } } \ No newline at end of file