瀏覽代碼

异常调用打印日志

andyliu 7 月之前
父節點
當前提交
344d53f38e

+ 28 - 3
src/main/java/com/shkpr/service/proxycenter/components/AbstractProxyServlet.java

@@ -1,5 +1,8 @@
 package com.shkpr.service.proxycenter.components;
 
+import com.global.base.log.LogLevelFlag;
+import com.global.base.log.LogPrintMgr;
+import com.shkpr.service.proxycenter.commtools.CommTool;
 import com.shkpr.service.proxycenter.commtools.HttpTool;
 import com.shkpr.service.proxycenter.constants.ApiURI;
 import com.shkpr.service.proxycenter.constants.ProxyPassDefine;
@@ -26,6 +29,8 @@ import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
 
 public abstract class AbstractProxyServlet extends ProxyServlet {
+    private String mStrClassName = "AbstractProxyServlet";
+    private String mStrBizType = "";
     private String proxyAddress = "https://127.0.0.1:9000/";
     private String proxyId = "";
     private String proxyTK = "";
@@ -74,17 +79,37 @@ public abstract class AbstractProxyServlet extends ProxyServlet {
 
     @Override
     protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
+        final String strUserAgent = servletRequest.getHeader(ApiURI.HEADER_USER_AGENT);
+        final String strClientType = servletRequest.getHeader(ApiURI.HEADER_CLIENT_TYPE);
+        final String strPlatform = CommTool.getPlatformByAgent(strClientType, strUserAgent);
+        final String strIpAddress = HttpTool.getIpAddress(servletRequest);
+        final String destUri = servletRequest.getRequestURI();
         ResponseCode headerCheck = checkHeader(servletRequest, servletResponse);
         if (headerCheck != ResponseCode.RESULT_NORMAL) {
+            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_WARN, proxyId, mStrClassName, "Outer"
+                    ,String.format("from:{%s-->%s} uri:{%s} failed:{code=%s,reason=%s}"
+                            ,strIpAddress
+                            ,strPlatform
+                            ,destUri
+                            ,headerCheck.toStrCode()
+                            ,headerCheck.toStrMsg()));
             HttpTool.handlerHttpErrorStatus(servletResponse, headerCheck);
             return;
         }
 
-        final String destUri = servletRequest.getRequestURI();
+
         servletRequest.setAttribute(ATTR_TARGET_URI, null);
 
         if (!destUri.startsWith(proxyUri)){
-            HttpTool.handlerHttpErrorStatus(servletResponse, ResponseCode.STATUS_NOT_FOUND_URI);
+            headerCheck = ResponseCode.STATUS_NOT_FOUND_URI;
+            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_WARN, proxyId, mStrClassName, "Inner"
+                    ,String.format("from:{%s-->%s} uri:{%s} failed:{code=%s,reason=%s}"
+                            ,strIpAddress
+                            ,strPlatform
+                            ,destUri
+                            ,headerCheck.toStrCode()
+                            ,headerCheck.toStrMsg()));
+            HttpTool.handlerHttpErrorStatus(servletResponse, headerCheck);
             return;
         }
 
@@ -109,7 +134,7 @@ public abstract class AbstractProxyServlet extends ProxyServlet {
             e.printStackTrace();
         }
         servletRequest.setAttribute(ATTR_TARGET_HOST, null);
-        super.targetHost = URIUtils.extractHost(uri);
+        super.targetHost = URIUtils.extractHost(uri);//只提取IP(域名)和端口,多余的路由无效
         super.service(servletRequest, servletResponse);
     }
 

+ 9 - 9
src/main/java/com/shkpr/service/proxycenter/components/GeoServerProxyServlet.java

@@ -38,21 +38,21 @@ public class GeoServerProxyServlet extends AbstractProxyServlet {
         String strSign = servletRequest.getHeader(ApiURI.HEADER_SIGNATURE);
         String strTime = servletRequest.getHeader(ApiURI.HEADER_TIMESTAMP);
         if (StringUtils.isEmpty(strSign) || StringUtils.isEmpty(strTime))
-            return ResponseCode.STATUS_DENY_OPERATE_USER;
-
+            return ResponseCode.RESULT_ERROR_SIGN;
+        
         long reqUTC = CastUtil.castUTCLong(strTime, 0L);
         if (!(TimeTool.isMsUTC(reqUTC) && Math.abs(System.currentTimeMillis()-reqUTC) <= 5*TimeTool.MS_ONE_MIN)){
-            return ResponseCode.STATUS_DENY_OPERATE_USER;
+            return ResponseCode.RESULT_REQUEST_TIMEOUT;
         }
 
-        String signDecode = AESUtil.AESDecrypt(AESUtil.Mode.CBC, AESUtil.Padding.PKCS5_PADDING, EncryptionUtil.MD5Hash(TokenAuthenticationService.SECRET+strTime).substring(8,24).toLowerCase(), strSign);
-        String needCheck = "";
+        String srcContent = "";
         try {
-            needCheck = new String(Base64.getEncoder().encode(strTime.getBytes()), "UTF-8");
+            srcContent = new String(Base64.getEncoder().encode(strTime.getBytes()), "UTF-8");
         }catch (Exception e){}
-
-        if (!signDecode.equals(needCheck))
-            return ResponseCode.STATUS_DENY_OPERATE_USER;
+        String ascKey = EncryptionUtil.MD5Hash(TokenAuthenticationService.SECRET+strTime).substring(8,24).toLowerCase();
+        String signDecode = AESUtil.AESDecrypt(AESUtil.Mode.CBC, AESUtil.Padding.PKCS5_PADDING, ascKey, strSign);
+        if (StringUtils.isEmpty(signDecode) || !signDecode.equals(srcContent))
+            return ResponseCode.RESULT_ERROR_SIGN;
         return ResponseCode.RESULT_NORMAL;
     }
 }