This commit is contained in:
shikong 2024-04-17 19:38:38 +08:00
parent 1d716448e7
commit e6bce05b67
2 changed files with 116 additions and 8 deletions

View File

@ -0,0 +1,97 @@
package cn.skcks.wx.official.api.advice;
import cn.skcks.wx.official.common.json.JsonResponse;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import java.util.Objects;
/**
* 全局异常处理类
*
* @author shikong
* @since 2024/4/17
*/
@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice
public class ExceptionAdvice {
@ExceptionHandler(MissingServletRequestParameterException.class)
public JsonResponse<String> missingServletRequestParameterException(MissingServletRequestParameterException e) {
return JsonResponse.error(e.getMessage());
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public JsonResponse<String> httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){
return JsonResponse.error(e.getMessage());
}
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public JsonResponse<String> unsupportedMediaTypeException(Exception e) {
e.printStackTrace();
return JsonResponse.error(e.getMessage());
}
@ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
public JsonResponse<String> httpMediaTypeNotAcceptableException(HttpMediaTypeNotAcceptableException e){
return JsonResponse.error(e.getMessage());
}
@ExceptionHandler(RuntimeException.class)
public JsonResponse<String> runtimeException(RuntimeException e) {
e.printStackTrace();
return JsonResponse.error(e.getMessage());
}
@ExceptionHandler(BindException.class)
public JsonResponse<String> handleValidationBindException(BindException e) {
return JsonResponse.error(Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public JsonResponse<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
return JsonResponse.error(Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage());
}
@ExceptionHandler(ConstraintViolationException.class)
public JsonResponse<String> handleConstraintViolationException(ConstraintViolationException e) {
return JsonResponse.error(Objects.requireNonNull(e.getMessage()));
}
@ExceptionHandler(HttpMessageNotReadableException.class)
public JsonResponse<String> handleHttpMessageNotReadableException(HttpMessageNotReadableException e){
log.warn("{}", e.getMessage());
return JsonResponse.error("参数异常");
}
@ExceptionHandler(AsyncRequestTimeoutException.class)
public JsonResponse<String> exception(AsyncRequestTimeoutException e) {
e.printStackTrace();
return JsonResponse.error("请求超时");
}
@ExceptionHandler(NullPointerException.class)
public JsonResponse<String> exception(NullPointerException e) {
e.printStackTrace();
return JsonResponse.error(e.getMessage());
}
@ExceptionHandler(Exception.class)
public JsonResponse<String> exception(Exception e) {
e.printStackTrace();
return JsonResponse.error(e.getMessage());
}
}

View File

@ -8,6 +8,7 @@ import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
@ -47,19 +48,20 @@ public class WxMpGenericService {
public void auth(String appId, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
log.debug("appId: {}", appId);
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
WxMpService wxMpService = wxMpServiceMap.get(appId);
if(wxMpService == null){
writeResponse(response, "未找到对应公众号配置");
return;
}
String signature = request.getParameter("signature");
String nonce = request.getParameter("nonce");
String timestamp = request.getParameter("timestamp");
WxMpService wxMpService = wxMpServiceMap.get(appId);
if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
log.error("非法请求,签名验证失败");
// 消息签名不正确说明不是公众平台发过来的消息
response.getWriter().println("非法请求");
writeResponse(response, "非法请求");
return;
}
@ -67,7 +69,7 @@ public class WxMpGenericService {
if (StringUtils.isNotBlank(echostr)) {
log.info("微信公众号平台验证请求 echostr: {}", echostr);
// 说明是一个仅仅用来验证的请求回显 echostr
response.getWriter().println(echostr);
writeResponse(response, echostr);
return;
}
@ -81,7 +83,8 @@ public class WxMpGenericService {
// WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
// if(outMessage == null) {
//为null说明路由配置有问题需要注意
response.getWriter().write("");
writeResponse(response, "");
// response.getWriter().write("");
// }
// response.getWriter().write(outMessage.toXml());
return;
@ -94,7 +97,8 @@ public class WxMpGenericService {
// WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
// if(outMessage == null) {
//为null说明路由配置有问题需要注意
response.getWriter().write("");
// response.getWriter().write("");
writeResponse(response, "");
// }
// response.getWriter().write(outMessage.toEncryptedXml(wxMpConfigStorage));
return;
@ -102,4 +106,11 @@ public class WxMpGenericService {
response.getWriter().println("不可识别的加密类型");
}
@SneakyThrows
private void writeResponse(HttpServletResponse response,String content) {
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().write(content);
}
}