|
@@ -1,14 +1,5 @@
|
|
1
|
package com.ruoyi.common.security.handler;
|
1
|
package com.ruoyi.common.security.handler;
|
|
2
|
|
2
|
|
|
3
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
|
4
|
-import org.slf4j.Logger;
|
|
|
|
5
|
-import org.slf4j.LoggerFactory;
|
|
|
|
6
|
-import org.springframework.validation.BindException;
|
|
|
|
7
|
-import org.springframework.web.HttpRequestMethodNotSupportedException;
|
|
|
|
8
|
-import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
|
9
|
-import org.springframework.web.bind.MissingPathVariableException;
|
|
|
|
10
|
-import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
11
|
-import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
12
|
import com.ruoyi.common.core.constant.HttpStatus;
|
3
|
import com.ruoyi.common.core.constant.HttpStatus;
|
|
13
|
import com.ruoyi.common.core.exception.DemoModeException;
|
4
|
import com.ruoyi.common.core.exception.DemoModeException;
|
|
14
|
import com.ruoyi.common.core.exception.InnerAuthException;
|
5
|
import com.ruoyi.common.core.exception.InnerAuthException;
|
|
@@ -17,24 +8,32 @@ import com.ruoyi.common.core.exception.auth.NotPermissionException;
|
|
17
|
import com.ruoyi.common.core.exception.auth.NotRoleException;
|
8
|
import com.ruoyi.common.core.exception.auth.NotRoleException;
|
|
18
|
import com.ruoyi.common.core.utils.StringUtils;
|
9
|
import com.ruoyi.common.core.utils.StringUtils;
|
|
19
|
import com.ruoyi.common.core.web.domain.AjaxResult;
|
10
|
import com.ruoyi.common.core.web.domain.AjaxResult;
|
|
|
|
11
|
+import org.slf4j.Logger;
|
|
|
|
12
|
+import org.slf4j.LoggerFactory;
|
|
|
|
13
|
+import org.springframework.validation.BindException;
|
|
|
|
14
|
+import org.springframework.web.HttpRequestMethodNotSupportedException;
|
|
|
|
15
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
|
16
|
+import org.springframework.web.bind.MissingPathVariableException;
|
|
|
|
17
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
18
|
+import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
20
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
19
|
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
|
21
|
|
20
|
|
|
|
|
21
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
22
|
+
|
|
22
|
/**
|
23
|
/**
|
|
23
|
* 全局异常处理器
|
24
|
* 全局异常处理器
|
|
24
|
- *
|
|
|
|
|
|
25
|
+ *
|
|
25
|
* @author ruoyi
|
26
|
* @author ruoyi
|
|
26
|
*/
|
27
|
*/
|
|
27
|
@RestControllerAdvice
|
28
|
@RestControllerAdvice
|
|
28
|
-public class GlobalExceptionHandler
|
|
|
|
29
|
-{
|
|
|
|
|
|
29
|
+public class GlobalExceptionHandler {
|
|
30
|
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
30
|
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
|
31
|
|
31
|
|
|
32
|
/**
|
32
|
/**
|
|
33
|
* 权限码异常
|
33
|
* 权限码异常
|
|
34
|
*/
|
34
|
*/
|
|
35
|
@ExceptionHandler(NotPermissionException.class)
|
35
|
@ExceptionHandler(NotPermissionException.class)
|
|
36
|
- public AjaxResult handleNotPermissionException(NotPermissionException e, HttpServletRequest request)
|
|
|
|
37
|
- {
|
|
|
|
|
|
36
|
+ public AjaxResult handleNotPermissionException(NotPermissionException e, HttpServletRequest request) {
|
|
38
|
String requestURI = request.getRequestURI();
|
37
|
String requestURI = request.getRequestURI();
|
|
39
|
log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage());
|
38
|
log.error("请求地址'{}',权限码校验失败'{}'", requestURI, e.getMessage());
|
|
40
|
return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权");
|
39
|
return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权");
|
|
@@ -44,8 +43,7 @@ public class GlobalExceptionHandler
|
|
44
|
* 角色权限异常
|
43
|
* 角色权限异常
|
|
45
|
*/
|
44
|
*/
|
|
46
|
@ExceptionHandler(NotRoleException.class)
|
45
|
@ExceptionHandler(NotRoleException.class)
|
|
47
|
- public AjaxResult handleNotRoleException(NotRoleException e, HttpServletRequest request)
|
|
|
|
48
|
- {
|
|
|
|
|
|
46
|
+ public AjaxResult handleNotRoleException(NotRoleException e, HttpServletRequest request) {
|
|
49
|
String requestURI = request.getRequestURI();
|
47
|
String requestURI = request.getRequestURI();
|
|
50
|
log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage());
|
48
|
log.error("请求地址'{}',角色权限校验失败'{}'", requestURI, e.getMessage());
|
|
51
|
return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权");
|
49
|
return AjaxResult.error(HttpStatus.FORBIDDEN, "没有访问权限,请联系管理员授权");
|
|
@@ -56,8 +54,7 @@ public class GlobalExceptionHandler
|
|
56
|
*/
|
54
|
*/
|
|
57
|
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
55
|
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
|
58
|
public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
|
56
|
public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e,
|
|
59
|
- HttpServletRequest request)
|
|
|
|
60
|
- {
|
|
|
|
|
|
57
|
+ HttpServletRequest request) {
|
|
61
|
String requestURI = request.getRequestURI();
|
58
|
String requestURI = request.getRequestURI();
|
|
62
|
log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
|
59
|
log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
|
|
63
|
return AjaxResult.error(e.getMessage());
|
60
|
return AjaxResult.error(e.getMessage());
|
|
@@ -67,8 +64,7 @@ public class GlobalExceptionHandler
|
|
67
|
* 业务异常
|
64
|
* 业务异常
|
|
68
|
*/
|
65
|
*/
|
|
69
|
@ExceptionHandler(ServiceException.class)
|
66
|
@ExceptionHandler(ServiceException.class)
|
|
70
|
- public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request)
|
|
|
|
71
|
- {
|
|
|
|
|
|
67
|
+ public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) {
|
|
72
|
log.error(e.getMessage(), e);
|
68
|
log.error(e.getMessage(), e);
|
|
73
|
Integer code = e.getCode();
|
69
|
Integer code = e.getCode();
|
|
74
|
return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
|
70
|
return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
|
|
@@ -98,8 +94,7 @@ public class GlobalExceptionHandler
|
|
98
|
* 拦截未知的运行时异常
|
94
|
* 拦截未知的运行时异常
|
|
99
|
*/
|
95
|
*/
|
|
100
|
@ExceptionHandler(RuntimeException.class)
|
96
|
@ExceptionHandler(RuntimeException.class)
|
|
101
|
- public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request)
|
|
|
|
102
|
- {
|
|
|
|
|
|
97
|
+ public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) {
|
|
103
|
String requestURI = request.getRequestURI();
|
98
|
String requestURI = request.getRequestURI();
|
|
104
|
log.error("请求地址'{}',发生未知异常.", requestURI, e);
|
99
|
log.error("请求地址'{}',发生未知异常.", requestURI, e);
|
|
105
|
return AjaxResult.error(e.getMessage());
|
100
|
return AjaxResult.error(e.getMessage());
|
|
@@ -109,8 +104,7 @@ public class GlobalExceptionHandler
|
|
109
|
* 系统异常
|
104
|
* 系统异常
|
|
110
|
*/
|
105
|
*/
|
|
111
|
@ExceptionHandler(Exception.class)
|
106
|
@ExceptionHandler(Exception.class)
|
|
112
|
- public AjaxResult handleException(Exception e, HttpServletRequest request)
|
|
|
|
113
|
- {
|
|
|
|
|
|
107
|
+ public AjaxResult handleException(Exception e, HttpServletRequest request) {
|
|
114
|
String requestURI = request.getRequestURI();
|
108
|
String requestURI = request.getRequestURI();
|
|
115
|
log.error("请求地址'{}',发生系统异常.", requestURI, e);
|
109
|
log.error("请求地址'{}',发生系统异常.", requestURI, e);
|
|
116
|
return AjaxResult.error(e.getMessage());
|
110
|
return AjaxResult.error(e.getMessage());
|
|
@@ -120,8 +114,7 @@ public class GlobalExceptionHandler
|
|
120
|
* 自定义验证异常
|
114
|
* 自定义验证异常
|
|
121
|
*/
|
115
|
*/
|
|
122
|
@ExceptionHandler(BindException.class)
|
116
|
@ExceptionHandler(BindException.class)
|
|
123
|
- public AjaxResult handleBindException(BindException e)
|
|
|
|
124
|
- {
|
|
|
|
|
|
117
|
+ public AjaxResult handleBindException(BindException e) {
|
|
125
|
log.error(e.getMessage(), e);
|
118
|
log.error(e.getMessage(), e);
|
|
126
|
String message = e.getAllErrors().get(0).getDefaultMessage();
|
119
|
String message = e.getAllErrors().get(0).getDefaultMessage();
|
|
127
|
return AjaxResult.error(message);
|
120
|
return AjaxResult.error(message);
|
|
@@ -131,8 +124,7 @@ public class GlobalExceptionHandler
|
|
131
|
* 自定义验证异常
|
124
|
* 自定义验证异常
|
|
132
|
*/
|
125
|
*/
|
|
133
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
126
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
134
|
- public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e)
|
|
|
|
135
|
- {
|
|
|
|
|
|
127
|
+ public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
|
136
|
log.error(e.getMessage(), e);
|
128
|
log.error(e.getMessage(), e);
|
|
137
|
String message = e.getBindingResult().getFieldError().getDefaultMessage();
|
129
|
String message = e.getBindingResult().getFieldError().getDefaultMessage();
|
|
138
|
return AjaxResult.error(message);
|
130
|
return AjaxResult.error(message);
|
|
@@ -142,8 +134,7 @@ public class GlobalExceptionHandler
|
|
142
|
* 内部认证异常
|
134
|
* 内部认证异常
|
|
143
|
*/
|
135
|
*/
|
|
144
|
@ExceptionHandler(InnerAuthException.class)
|
136
|
@ExceptionHandler(InnerAuthException.class)
|
|
145
|
- public AjaxResult handleInnerAuthException(InnerAuthException e)
|
|
|
|
146
|
- {
|
|
|
|
|
|
137
|
+ public AjaxResult handleInnerAuthException(InnerAuthException e) {
|
|
147
|
return AjaxResult.error(e.getMessage());
|
138
|
return AjaxResult.error(e.getMessage());
|
|
148
|
}
|
139
|
}
|
|
149
|
|
140
|
|
|
@@ -151,8 +142,7 @@ public class GlobalExceptionHandler
|
|
151
|
* 演示模式异常
|
142
|
* 演示模式异常
|
|
152
|
*/
|
143
|
*/
|
|
153
|
@ExceptionHandler(DemoModeException.class)
|
144
|
@ExceptionHandler(DemoModeException.class)
|
|
154
|
- public AjaxResult handleDemoModeException(DemoModeException e)
|
|
|
|
155
|
- {
|
|
|
|
|
|
145
|
+ public AjaxResult handleDemoModeException(DemoModeException e) {
|
|
156
|
return AjaxResult.error("演示模式,不允许操作");
|
146
|
return AjaxResult.error("演示模式,不允许操作");
|
|
157
|
}
|
147
|
}
|
|
158
|
}
|
148
|
}
|