From d1d5c0f54d55fa4413ba2533f0bb01ae870a1304 Mon Sep 17 00:00:00 2001 From: cuibaocheng Date: Thu, 16 Sep 2021 14:23:01 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=A2=9E=E5=8A=A0=E4=BA=86WebSocket?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E4=B8=8E=E5=BC=82=E5=B8=B8=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../systemcard/filter/CardAppException.java | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/main/java/cn/com/tenlion/systemcard/filter/CardAppException.java diff --git a/src/main/java/cn/com/tenlion/systemcard/filter/CardAppException.java b/src/main/java/cn/com/tenlion/systemcard/filter/CardAppException.java new file mode 100644 index 0000000..bbecf76 --- /dev/null +++ b/src/main/java/cn/com/tenlion/systemcard/filter/CardAppException.java @@ -0,0 +1,76 @@ +package cn.com.tenlion.systemcard.filter; + +import cn.com.tenlion.systemcard.socket.CardLogSocket; +import ink.wgink.common.component.SecurityComponent; +import ink.wgink.pojo.app.AppTokenUser; +import ink.wgink.util.ReflectUtil; +import ink.wgink.util.date.DateUtil; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.servlet.HandlerExceptionResolver; +import org.springframework.web.servlet.ModelAndView; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +@ControllerAdvice +public class CardAppException { + + @Autowired + CardLogSocket cardLogSocket; + + @Autowired + protected SecurityComponent securityComponent; + + @ExceptionHandler(Exception.class) + public ModelAndView customException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Exception e) { + System.out.println(e.getMessage()); + e.printStackTrace(); + HttpServletRequest httpRequest = (HttpServletRequest) httpServletRequest; + String token = httpRequest.getHeader("token"); + String url = httpRequest.getRequestURI(); + if(!url.contains("/app/")) { + return null; + } + if(!StringUtils.isEmpty(token) && !StringUtils.isEmpty(cardLogSocket.selectUser)) { + AppTokenUser appTokenUser = null; + try { + appTokenUser = securityComponent.getAppTokenUser(token); + } catch (ReflectUtil.ReflectException e1) { + } + if(appTokenUser == null || !cardLogSocket.selectUser.equals(appTokenUser.getId())) { + return null; + } + } + // 此方式支持form-data传参方式参数获取,其他传参方式请自行研究 + Map parameterMap = httpRequest.getParameterMap(); + List parameterList = new ArrayList<>(); + parameterMap.forEach((key, value) -> { + parameterList.add(key + ":" + Arrays.toString(value)); + }); + String clientIp = httpRequest.getHeader("x-forwarded-for"); + if (clientIp == null) { + clientIp = httpRequest.getRemoteAddr(); + } + if(cardLogSocket.exceptionDescription) { + StringWriter trace = new StringWriter(); + e.printStackTrace(new PrintWriter(trace)); + String log = "Exception:" + e.getMessage() + "\r\n" + trace.toString(); + cardLogSocket.sendAll(log); + }else{ + String log = "Exception:" + e.getMessage(); + cardLogSocket.sendAll(log); + } + return null; + } +} +