首页>代码>Spring Boot-Shiro-Vue权限管理 - 分享>/SpringBoot-Shiro-Vue/back/src/main/java/com/heeexy/example/config/exception/GlobalExceptionHandler.java
package com.heeexy.example.config.exception; import com.alibaba.fastjson.JSONObject; import com.heeexy.example.util.CommonUtil; import com.heeexy.example.util.constants.ErrorEnum; import org.apache.shiro.authz.UnauthenticatedException; import org.apache.shiro.authz.UnauthorizedException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; /** * @author: hxy * @description: 统一异常拦截 * @date: 2017/10/24 10:31 */ @ControllerAdvice @ResponseBody public class GlobalExceptionHandler { private Logger logger = LoggerFactory.getLogger(this.getClass().getName()); @ExceptionHandler(value = Exception.class) public JSONObject defaultErrorHandler(HttpServletRequest req, Exception e) { String errorPosition = ""; //如果错误堆栈信息存在 if (e.getStackTrace().length > 0) { StackTraceElement element = e.getStackTrace()[0]; String fileName = element.getFileName() == null ? "未找到错误文件" : element.getFileName(); int lineNumber = element.getLineNumber(); errorPosition = fileName + ":" + lineNumber; } JSONObject jsonObject = new JSONObject(); jsonObject.put("code", ErrorEnum.E_400.getErrorCode()); jsonObject.put("msg", ErrorEnum.E_400.getErrorMsg()); JSONObject errorObject = new JSONObject(); errorObject.put("errorLocation", e.toString() + " 错误位置:" + errorPosition); jsonObject.put("info", errorObject); logger.error("异常", e); return jsonObject; } /** * GET/POST请求方法错误的拦截器 * 因为开发时可能比较常见,而且发生在进入controller之前,上面的拦截器拦截不到这个错误 * 所以定义了这个拦截器 */ @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public JSONObject httpRequestMethodHandler() { return CommonUtil.errorJson(ErrorEnum.E_500); } /** * 本系统自定义错误的拦截器 * 拦截到此错误之后,就返回这个类里面的json给前端 * 常见使用场景是参数校验失败,抛出此错,返回错误信息给前端 */ @ExceptionHandler(CommonJsonException.class) public JSONObject commonJsonExceptionHandler(CommonJsonException commonJsonException) { return commonJsonException.getResultJson(); } /** * 权限不足报错拦截 */ @ExceptionHandler(UnauthorizedException.class) public JSONObject unauthorizedExceptionHandler() { return CommonUtil.errorJson(ErrorEnum.E_502); } /** * 未登录报错拦截 * 在请求需要权限的接口,而连登录都还没登录的时候,会报此错 */ @ExceptionHandler(UnauthenticatedException.class) public JSONObject unauthenticatedException() { return CommonUtil.errorJson(ErrorEnum.E_20011); } }


木子520 LV12
5月14日
gann2015 LV25
4月8日
微信网友_6955249237250048 LV5
1月3日
543666826 LV34
2024年12月28日
ma406805131 LV19
2024年12月28日
叫我上帝 LV2
2024年12月19日
张春青 LV6
2024年12月16日
18135724357
2024年10月12日
暂无贡献等级
sweetlove LV20
2024年7月30日
qiangmin1223 LV12
2024年4月24日