GloableExceptionResolver.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package com.usky.exception;
  2. import com.usky.utils.Result;
  3. import com.usky.utils.ServletUtils;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.apache.shiro.authz.AuthorizationException;
  6. import org.apache.shiro.util.PermissionUtils;
  7. import org.springframework.web.HttpRequestMethodNotSupportedException;
  8. import org.springframework.web.bind.annotation.ExceptionHandler;
  9. import org.springframework.web.bind.annotation.RestControllerAdvice;
  10. import org.springframework.web.servlet.ModelAndView;
  11. import javax.servlet.http.HttpServletRequest;
  12. /**
  13. * @author laowo
  14. * @version v1.0
  15. * @date 2020/11/10 15:00
  16. * @description 全局异常处理
  17. **/
  18. @RestControllerAdvice
  19. @Slf4j
  20. public class GloableExceptionResolver {
  21. // @ExceptionHandler(UnauthorizedException.class)
  22. // public void calUnauthorizedException(UnauthorizedException e) {
  23. // PrintWriter writer = null;
  24. // try {
  25. // //判断是否是ajax
  26. // ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  27. // HttpServletRequest request = requestAttributes.getRequest();
  28. // HttpServletResponse response = requestAttributes.getResponse();
  29. // String header = request.getHeader("X-Requested-With");
  30. // if (StringUtils.isNoneBlank(header) && "XMLHttpRequest".equalsIgnoreCase(header)) {
  31. // response.setCharacterEncoding("UTF-8");
  32. // response.setContentType("application/json; charset=utf-8");
  33. // writer = response.getWriter();
  34. // writer.write("{\"status\":401,\"message\":\"无权访问\"}");
  35. // } else {
  36. // String contextPath = request.getContextPath();
  37. // if ("/".equals(contextPath))
  38. // contextPath = "";
  39. // response.sendRedirect(request.getContextPath() + "/page/toDenied");
  40. // }
  41. // } catch (IOException io) {
  42. // io.printStackTrace();
  43. // } finally {
  44. // if (writer != null)
  45. // writer.close();
  46. // }
  47. // }
  48. //
  49. // @ExceptionHandler(UnauthenticatedException.class)
  50. // public void calUnauthorizedException(UnauthenticatedException e) {
  51. // PrintWriter writer = null;
  52. // try {
  53. // //判断是否是异步请求
  54. // ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  55. // HttpServletRequest request = requestAttributes.getRequest();
  56. // HttpServletResponse response = requestAttributes.getResponse();
  57. // String header = request.getHeader("X-Requested-With");
  58. // if (StringUtils.isNoneBlank(header) && "XMLHttpRequest".equalsIgnoreCase(header)) {
  59. // response.setCharacterEncoding("UTF-8");
  60. // response.setContentType("application/json; charset=utf-8");
  61. // writer = response.getWriter();
  62. // writer.write("{\"status\":302,\"message\":\"请前去登录\"}");
  63. // } else {
  64. // String contextPath = request.getContextPath();
  65. // if ("/".equals(contextPath))
  66. // contextPath = "";
  67. // response.sendRedirect(request.getContextPath() + "/login/");
  68. // }
  69. // } catch (IOException io) {
  70. // io.printStackTrace();
  71. // } finally {
  72. // if (writer != null)
  73. // writer.close();
  74. // }
  75. // }
  76. //
  77. //
  78. // @ExceptionHandler(BaseException.class)
  79. // public Object businessException(HttpServletRequest request, BaseException e) {
  80. // log.error(e.getMessage(), e);
  81. // if (ServletUtils.isAjaxRequest(request)) {
  82. // return Result.error(e.getMessage());
  83. // } else {
  84. //
  85. // // ModelAndView modelAndView = new ModelAndView(); //TODO 异常页面跳转设置
  86. // // modelAndView.addObject("errorMessage", e.getMessage());
  87. // // modelAndView.setViewName("error/business");
  88. // // return modelAndView;
  89. //
  90. // return null;
  91. // }
  92. // }
  93. //
  94. // @ExceptionHandler(UnavailableSecurityManagerException.class)
  95. // @ResponseBody
  96. // public Object UnavailableSecurityManagerException(HttpServletRequest request, BaseException e) {
  97. // log.error(e.getMessage(), e);
  98. // if (ServletUtils.isAjaxRequest(request)) {
  99. // return Result.error("用户信息异常,请重新登录");
  100. // } else {
  101. //
  102. // // ModelAndView modelAndView = new ModelAndView(); //TODO 异常页面跳转设置
  103. // // modelAndView.addObject("errorMessage", e.getMessage());
  104. // // modelAndView.setViewName("error/business");
  105. // // return modelAndView;
  106. //
  107. // return null;
  108. // }
  109. // }
  110. //
  111. //
  112. /**
  113. * 权限校验失败 如果请求为ajax返回json,普通请求跳转页面
  114. */
  115. @ExceptionHandler(AuthorizationException.class)
  116. public Object handleAuthorizationException(HttpServletRequest request, AuthorizationException e)
  117. {
  118. log.error(e.getMessage(), e);
  119. if (ServletUtils.isAjaxRequest(request))
  120. {
  121. return Result.error("权限错误!");
  122. }
  123. else
  124. {
  125. // ModelAndView modelAndView = new ModelAndView();
  126. // modelAndView.setViewName("error/unauth");
  127. // return modelAndView;
  128. return Result.error("权限错误!");
  129. }
  130. }
  131. /**
  132. * 请求方式不支持
  133. */
  134. @ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
  135. public Result handleException(HttpRequestMethodNotSupportedException e)
  136. {
  137. log.error(e.getMessage(), e);
  138. return Result.error("不支持' " + e.getMethod() + "'请求");
  139. }
  140. /**
  141. * 拦截未知的运行时异常
  142. */
  143. @ExceptionHandler(RuntimeException.class)
  144. public Result notFount(RuntimeException e)
  145. {
  146. log.error("运行时异常:", e);
  147. return Result.error("运行时异常:" + e.getMessage());
  148. }
  149. /**
  150. * 系统异常
  151. */
  152. @ExceptionHandler(Exception.class)
  153. public Result handleException(Exception e)
  154. {
  155. log.error(e.getMessage(), e);
  156. return Result.error("服务器错误,请联系管理员");
  157. }
  158. /**
  159. * 业务异常
  160. */
  161. @ExceptionHandler(BusinessException.class)
  162. public Object businessException(HttpServletRequest request, BusinessException e)
  163. {
  164. log.error(e.getMessage(), e);
  165. if (ServletUtils.isAjaxRequest(request))
  166. {
  167. return Result.error(e.getMessage());
  168. }
  169. else
  170. {
  171. // ModelAndView modelAndView = new ModelAndView();
  172. // modelAndView.addObject("errorMessage", e.getMessage());
  173. // modelAndView.setViewName("error/business");
  174. // return modelAndView;
  175. return Result.error(e.getMessage());
  176. }
  177. }
  178. }