| | |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.security.access.AccessDeniedException; |
| | | import org.springframework.context.support.DefaultMessageSourceResolvable; |
| | | import org.springframework.validation.BindException; |
| | | import org.springframework.web.HttpRequestMethodNotSupportedException; |
| | | import org.springframework.web.bind.MethodArgumentNotValidException; |
| | |
| | | import org.springframework.web.bind.annotation.RestControllerAdvice; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.validation.ConstraintViolation; |
| | | import javax.validation.ConstraintViolationException; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 全局异常处理器 |
| | |
| | | * 角色校验异常 |
| | | */ |
| | | @ExceptionHandler(NotRoleException.class) |
| | | public AjaxResult handleAccessDeniedException(NotRoleException e, HttpServletRequest request) |
| | | { |
| | | public AjaxResult<Void> handleAccessDeniedException(NotRoleException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',角色校验失败'{}'", requestURI, e.getMessage()); |
| | | return AjaxResult.error(HttpStatus.HTTP_FORBIDDEN, "没有角色,请联系管理员授权"); |
| | |
| | | * 认证失败 |
| | | */ |
| | | @ExceptionHandler(NotLoginException.class) |
| | | public AjaxResult handleAccessDeniedException(NotLoginException e, HttpServletRequest request) |
| | | { |
| | | public AjaxResult<Void> handleAccessDeniedException(NotLoginException e, HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求访问:{},认证失败,无法访问系统资源", requestURI, e.getMessage()); |
| | | return AjaxResult.error(HttpStatus.HTTP_UNAUTHORIZED, StringUtils.format("请求访问:{},认证失败,无法访问系统资源", requestURI)); |
| | | log.error("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI, e.getMessage()); |
| | | return AjaxResult.error(HttpStatus.HTTP_UNAUTHORIZED, StringUtils.format("请求地址'{}',认证失败'{}',无法访问系统资源", requestURI)); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @ExceptionHandler(HttpRequestMethodNotSupportedException.class) |
| | | public AjaxResult<Void> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, |
| | | HttpServletRequest request) { |
| | | HttpServletRequest request) { |
| | | String requestURI = request.getRequestURI(); |
| | | log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod()); |
| | | return AjaxResult.error(e.getMessage()); |
| | |
| | | @ExceptionHandler(BindException.class) |
| | | public AjaxResult<Void> handleBindException(BindException e) { |
| | | log.error(e.getMessage(), e); |
| | | String message = e.getAllErrors().get(0).getDefaultMessage(); |
| | | String message = e.getAllErrors().stream() |
| | | .map(DefaultMessageSourceResolvable::getDefaultMessage) |
| | | .collect(Collectors.joining(", ")); |
| | | return AjaxResult.error(message); |
| | | } |
| | | |
| | |
| | | @ExceptionHandler(ConstraintViolationException.class) |
| | | public AjaxResult<Void> constraintViolationException(ConstraintViolationException e) { |
| | | log.error(e.getMessage(), e); |
| | | String message = e.getConstraintViolations().iterator().next().getMessage(); |
| | | String message = e.getConstraintViolations().stream() |
| | | .map(ConstraintViolation::getMessage) |
| | | .collect(Collectors.joining(", ")); |
| | | return AjaxResult.error(message); |
| | | } |
| | | |