1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.yiboshi.science.config.exception;
import com.yiboshi.arch.base.ResponseCode;
import com.yiboshi.arch.base.ResponseDataModel;
import com.yiboshi.arch.exception.BusinessException;
import com.yiboshi.science.service.LogsExceptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import javax.servlet.http.HttpServletRequest;
/**
* class: GlobalExceptionHandler(全局异常处理)
* create by: dracula$
* description:
* create time: 2021-09-13$
*
* @param: $
*/
@ControllerAdvice
public class GlobalExceptionHandler {
@Autowired
private LogsExceptionService logsExceptionService;
/**
* 全局未知异常处理
*
* @param request
* @param handlerMethod
* @param exception
* @return
*/
@ExceptionHandler(Exception.class)
@ResponseBody
public ResponseDataModel handleException(HttpServletRequest request, HandlerMethod handlerMethod, Exception exception) {//HttpServletResponse response,
return AllExceptionHandler(request, handlerMethod, exception);
}
/**
* BadCredentialsException
*/
@ExceptionHandler(BadCredentialsException.class)
public ResponseDataModel badCredentialsException(HttpServletRequest request, HandlerMethod handlerMethod, Exception e) {
// 打印堆栈信息
String message = "坏的凭证".equals(e.getMessage()) ? "用户名或密码不正确" : e.getMessage();
return AllExceptionHandler(request, handlerMethod, e);
}
/**
* 处理所有业务异常
*
* @param exception
* @return
*/
@ExceptionHandler(BusinessException.class)
@ResponseBody
public ResponseDataModel handleBusinessException(HttpServletRequest request, HandlerMethod handlerMethod, BusinessException exception) {
return AllExceptionHandler(request, handlerMethod, exception);
}
/**
* 处理所有接口数据验证异常
*
* @param exception
* @return
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
public ResponseDataModel handleMethodArgumentNotValidException(HttpServletRequest request, HandlerMethod handlerMethod, MethodArgumentNotValidException exception) {
return AllExceptionHandler(request, handlerMethod, exception);
}
/**
* 异常分类处理
*
* @param request
* @param handlerMethod
* @param exception
* @return
*/
private ResponseDataModel AllExceptionHandler(HttpServletRequest request, HandlerMethod handlerMethod, Exception exception) {
String msg;
ResponseCode responseCode=ResponseCode.BUSSINESS_EXCEPTION;
if (exception instanceof BusinessException) {
msg = exception.getMessage();
} else if (exception instanceof BadCredentialsException) {
msg = "用户名或密码不正确";
} else if (exception instanceof InvalidGrantException) {
msg = "用户名或密码不正确";
} else if (exception instanceof UsernameNotFoundException) {
msg = "用户名或密码不正确";
} else if (exception instanceof InternalAuthenticationServiceException) {
msg = "账户不存在或已注销!";
} else if (exception instanceof DisabledException) {
msg = "账户不存在或已注销!";
} else if (exception instanceof AccessDeniedException) {
msg = "无权限访问";
} else if (exception instanceof MethodArgumentNotValidException) {
msg = "接口数据验证异常";
responseCode=ResponseCode.PARAMETER_EXCEPTION;
logsExceptionService.logs(handlerMethod,exception,"接口数据验证异常");
} else{
msg = exception.getMessage();
responseCode=ResponseCode.SERVER_ERROR;
logsExceptionService.logs(handlerMethod,exception,"500 Internal Server Error!");
}
return new ResponseDataModel(responseCode, msg);
}
/**
* 区分业务类和方法
*
* @param request
* @param handlerMethod
* @return
* @throws Exception
*/
private String decisionShortTitle(HttpServletRequest request, HandlerMethod handlerMethod) {
String className = handlerMethod.getMethod().getDeclaringClass().getName();
String MethodType = request.getMethod();
String MethodName = handlerMethod.getMethod().getName();
return className + ":" + MethodName + ":" + MethodType;
}
}