123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- package com.usky.exception;
- import com.usky.utils.Result;
- import com.usky.utils.ServletUtils;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.shiro.authz.AuthorizationException;
- import org.apache.shiro.util.PermissionUtils;
- import org.springframework.web.HttpRequestMethodNotSupportedException;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.RestControllerAdvice;
- import org.springframework.web.servlet.ModelAndView;
- import javax.servlet.http.HttpServletRequest;
- /**
- * @author laowo
- * @version v1.0
- * @date 2020/11/10 15:00
- * @description 全局异常处理
- **/
- @RestControllerAdvice
- @Slf4j
- public class GloableExceptionResolver {
- // @ExceptionHandler(UnauthorizedException.class)
- // public void calUnauthorizedException(UnauthorizedException e) {
- // PrintWriter writer = null;
- // try {
- // //判断是否是ajax
- // ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
- // HttpServletRequest request = requestAttributes.getRequest();
- // HttpServletResponse response = requestAttributes.getResponse();
- // String header = request.getHeader("X-Requested-With");
- // if (StringUtils.isNoneBlank(header) && "XMLHttpRequest".equalsIgnoreCase(header)) {
- // response.setCharacterEncoding("UTF-8");
- // response.setContentType("application/json; charset=utf-8");
- // writer = response.getWriter();
- // writer.write("{\"status\":401,\"message\":\"无权访问\"}");
- // } else {
- // String contextPath = request.getContextPath();
- // if ("/".equals(contextPath))
- // contextPath = "";
- // response.sendRedirect(request.getContextPath() + "/page/toDenied");
- // }
- // } catch (IOException io) {
- // io.printStackTrace();
- // } finally {
- // if (writer != null)
- // writer.close();
- // }
- // }
- //
- // @ExceptionHandler(UnauthenticatedException.class)
- // public void calUnauthorizedException(UnauthenticatedException e) {
- // PrintWriter writer = null;
- // try {
- // //判断是否是异步请求
- // ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
- // HttpServletRequest request = requestAttributes.getRequest();
- // HttpServletResponse response = requestAttributes.getResponse();
- // String header = request.getHeader("X-Requested-With");
- // if (StringUtils.isNoneBlank(header) && "XMLHttpRequest".equalsIgnoreCase(header)) {
- // response.setCharacterEncoding("UTF-8");
- // response.setContentType("application/json; charset=utf-8");
- // writer = response.getWriter();
- // writer.write("{\"status\":302,\"message\":\"请前去登录\"}");
- // } else {
- // String contextPath = request.getContextPath();
- // if ("/".equals(contextPath))
- // contextPath = "";
- // response.sendRedirect(request.getContextPath() + "/login/");
- // }
- // } catch (IOException io) {
- // io.printStackTrace();
- // } finally {
- // if (writer != null)
- // writer.close();
- // }
- // }
- //
- //
- // @ExceptionHandler(BaseException.class)
- // public Object businessException(HttpServletRequest request, BaseException e) {
- // log.error(e.getMessage(), e);
- // if (ServletUtils.isAjaxRequest(request)) {
- // return Result.error(e.getMessage());
- // } else {
- //
- // // ModelAndView modelAndView = new ModelAndView(); //TODO 异常页面跳转设置
- // // modelAndView.addObject("errorMessage", e.getMessage());
- // // modelAndView.setViewName("error/business");
- // // return modelAndView;
- //
- // return null;
- // }
- // }
- //
- // @ExceptionHandler(UnavailableSecurityManagerException.class)
- // @ResponseBody
- // public Object UnavailableSecurityManagerException(HttpServletRequest request, BaseException e) {
- // log.error(e.getMessage(), e);
- // if (ServletUtils.isAjaxRequest(request)) {
- // return Result.error("用户信息异常,请重新登录");
- // } else {
- //
- // // ModelAndView modelAndView = new ModelAndView(); //TODO 异常页面跳转设置
- // // modelAndView.addObject("errorMessage", e.getMessage());
- // // modelAndView.setViewName("error/business");
- // // return modelAndView;
- //
- // return null;
- // }
- // }
- //
- //
- /**
- * 权限校验失败 如果请求为ajax返回json,普通请求跳转页面
- */
- @ExceptionHandler(AuthorizationException.class)
- public Object handleAuthorizationException(HttpServletRequest request, AuthorizationException e)
- {
- log.error(e.getMessage(), e);
- if (ServletUtils.isAjaxRequest(request))
- {
- return Result.error("权限错误!");
- }
- else
- {
- // ModelAndView modelAndView = new ModelAndView();
- // modelAndView.setViewName("error/unauth");
- // return modelAndView;
- return Result.error("权限错误!");
- }
- }
- /**
- * 请求方式不支持
- */
- @ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
- public Result handleException(HttpRequestMethodNotSupportedException e)
- {
- log.error(e.getMessage(), e);
- return Result.error("不支持' " + e.getMethod() + "'请求");
- }
- /**
- * 拦截未知的运行时异常
- */
- @ExceptionHandler(RuntimeException.class)
- public Result notFount(RuntimeException e)
- {
- log.error("运行时异常:", e);
- return Result.error("运行时异常:" + e.getMessage());
- }
- /**
- * 系统异常
- */
- @ExceptionHandler(Exception.class)
- public Result handleException(Exception e)
- {
- log.error(e.getMessage(), e);
- return Result.error("服务器错误,请联系管理员");
- }
- /**
- * 业务异常
- */
- @ExceptionHandler(BusinessException.class)
- public Object businessException(HttpServletRequest request, BusinessException e)
- {
- log.error(e.getMessage(), e);
- if (ServletUtils.isAjaxRequest(request))
- {
- return Result.error(e.getMessage());
- }
- else
- {
- // ModelAndView modelAndView = new ModelAndView();
- // modelAndView.addObject("errorMessage", e.getMessage());
- // modelAndView.setViewName("error/business");
- // return modelAndView;
- return Result.error(e.getMessage());
- }
- }
- }
|