|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+package com.ruoyi.common.security.handler;
|
|
|
2
|
+
|
|
|
3
|
+import org.slf4j.Logger;
|
|
|
4
|
+import org.slf4j.LoggerFactory;
|
|
|
5
|
+import org.springframework.security.access.AccessDeniedException;
|
|
|
6
|
+import org.springframework.security.authentication.AccountExpiredException;
|
|
|
7
|
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
8
|
+import org.springframework.validation.BindException;
|
|
|
9
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
10
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
11
|
+import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
12
|
+import org.springframework.web.servlet.NoHandlerFoundException;
|
|
|
13
|
+import com.ruoyi.common.core.constant.HttpStatus;
|
|
|
14
|
+import com.ruoyi.common.core.exception.BaseException;
|
|
|
15
|
+import com.ruoyi.common.core.exception.CustomException;
|
|
|
16
|
+import com.ruoyi.common.core.exception.DemoModeException;
|
|
|
17
|
+import com.ruoyi.common.core.utils.StringUtils;
|
|
|
18
|
+import com.ruoyi.common.core.web.domain.AjaxResult;
|
|
|
19
|
+
|
|
|
20
|
+/**
|
|
|
21
|
+ * 全局异常处理器
|
|
|
22
|
+ *
|
|
|
23
|
+ * @author ruoyi
|
|
|
24
|
+ */
|
|
|
25
|
+@RestControllerAdvice
|
|
|
26
|
+public class GlobalExceptionHandler
|
|
|
27
|
+{
|
|
|
28
|
+ private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
|
|
29
|
+
|
|
|
30
|
+ /**
|
|
|
31
|
+ * 基础异常
|
|
|
32
|
+ */
|
|
|
33
|
+ @ExceptionHandler(BaseException.class)
|
|
|
34
|
+ public AjaxResult baseException(BaseException e)
|
|
|
35
|
+ {
|
|
|
36
|
+ return AjaxResult.error(e.getMessage());
|
|
|
37
|
+ }
|
|
|
38
|
+
|
|
|
39
|
+ /**
|
|
|
40
|
+ * 业务异常
|
|
|
41
|
+ */
|
|
|
42
|
+ @ExceptionHandler(CustomException.class)
|
|
|
43
|
+ public AjaxResult businessException(CustomException e)
|
|
|
44
|
+ {
|
|
|
45
|
+ if (StringUtils.isNull(e.getCode()))
|
|
|
46
|
+ {
|
|
|
47
|
+ return AjaxResult.error(e.getMessage());
|
|
|
48
|
+ }
|
|
|
49
|
+ return AjaxResult.error(e.getCode(), e.getMessage());
|
|
|
50
|
+ }
|
|
|
51
|
+
|
|
|
52
|
+ @ExceptionHandler(NoHandlerFoundException.class)
|
|
|
53
|
+ public AjaxResult handlerNoFoundException(Exception e)
|
|
|
54
|
+ {
|
|
|
55
|
+ log.error(e.getMessage(), e);
|
|
|
56
|
+ return AjaxResult.error(HttpStatus.NOT_FOUND, "路径不存在,请检查路径是否正确");
|
|
|
57
|
+ }
|
|
|
58
|
+
|
|
|
59
|
+ @ExceptionHandler(AccessDeniedException.class)
|
|
|
60
|
+ public AjaxResult handleAuthorizationException(AccessDeniedException e)
|
|
|
61
|
+ {
|
|
|
62
|
+ log.error(e.getMessage());
|
|
|
63
|
+ return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限,请联系管理员授权");
|
|
|
64
|
+ }
|
|
|
65
|
+
|
|
|
66
|
+ @ExceptionHandler(AccountExpiredException.class)
|
|
|
67
|
+ public AjaxResult handleAccountExpiredException(AccountExpiredException e)
|
|
|
68
|
+ {
|
|
|
69
|
+ log.error(e.getMessage(), e);
|
|
|
70
|
+ return AjaxResult.error(e.getMessage());
|
|
|
71
|
+ }
|
|
|
72
|
+
|
|
|
73
|
+ @ExceptionHandler(UsernameNotFoundException.class)
|
|
|
74
|
+ public AjaxResult handleUsernameNotFoundException(UsernameNotFoundException e)
|
|
|
75
|
+ {
|
|
|
76
|
+ log.error(e.getMessage(), e);
|
|
|
77
|
+ return AjaxResult.error(e.getMessage());
|
|
|
78
|
+ }
|
|
|
79
|
+
|
|
|
80
|
+ @ExceptionHandler(Exception.class)
|
|
|
81
|
+ public AjaxResult handleException(Exception e)
|
|
|
82
|
+ {
|
|
|
83
|
+ log.error(e.getMessage(), e);
|
|
|
84
|
+ return AjaxResult.error(e.getMessage());
|
|
|
85
|
+ }
|
|
|
86
|
+
|
|
|
87
|
+ /**
|
|
|
88
|
+ * 自定义验证异常
|
|
|
89
|
+ */
|
|
|
90
|
+ @ExceptionHandler(BindException.class)
|
|
|
91
|
+ public AjaxResult validatedBindException(BindException e)
|
|
|
92
|
+ {
|
|
|
93
|
+ log.error(e.getMessage(), e);
|
|
|
94
|
+ String message = e.getAllErrors().get(0).getDefaultMessage();
|
|
|
95
|
+ return AjaxResult.error(message);
|
|
|
96
|
+ }
|
|
|
97
|
+
|
|
|
98
|
+ /**
|
|
|
99
|
+ * 自定义验证异常
|
|
|
100
|
+ */
|
|
|
101
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
102
|
+ public Object validExceptionHandler(MethodArgumentNotValidException e)
|
|
|
103
|
+ {
|
|
|
104
|
+ log.error(e.getMessage(), e);
|
|
|
105
|
+ String message = e.getBindingResult().getFieldError().getDefaultMessage();
|
|
|
106
|
+ return AjaxResult.error(message);
|
|
|
107
|
+ }
|
|
|
108
|
+
|
|
|
109
|
+ /**
|
|
|
110
|
+ * 演示模式异常
|
|
|
111
|
+ */
|
|
|
112
|
+ @ExceptionHandler(DemoModeException.class)
|
|
|
113
|
+ public AjaxResult demoModeException(DemoModeException e)
|
|
|
114
|
+ {
|
|
|
115
|
+ return AjaxResult.error("演示模式,不允许操作");
|
|
|
116
|
+ }
|
|
|
117
|
+}
|