Authored by mali

压测后门

@@ -110,6 +110,14 @@ public class SecurityInterceptor implements HandlerInterceptor, ApplicationEvent @@ -110,6 +110,14 @@ public class SecurityInterceptor implements HandlerInterceptor, ApplicationEvent
110 String businessLine = params.get("business_line"); 110 String businessLine = params.get("business_line");
111 //==============以下是完全不校验的场景========================= 111 //==============以下是完全不校验的场景=========================
112 112
  113 + //1 后门, 不需要校验, (1)检查请求参数是否有预留的后门参数_sncp, 并且_sncp的值是有效的. 放行 _sncp的值再5.6版本会去掉
  114 + String temporaryValueQeq = params.get("_sncp"); //请求的预留后门的值
  115 + String temporaryValue = configReader.getString("gateway.client.secret.h5", "");
  116 + if (!StringUtils.isEmpty(temporaryValueQeq) && !StringUtils.isEmpty(temporaryValue) && temporaryValueQeq.trim().equals(temporaryValue.trim()) && isInnerIp(httpServletRequest)){
  117 + return ;
  118 + }
  119 +
  120 +
113 //2 是否校验全部接口,开关-true:校验全部接口(除去@IgnoreSession注解接口) 开关-false:只校验核心接口 121 //2 是否校验全部接口,开关-true:校验全部接口(除去@IgnoreSession注解接口) 开关-false:只校验核心接口
114 boolean isVerifyAllMethod = configReader.getBoolean("gateway.session.isVerifyAllMethod", true); 122 boolean isVerifyAllMethod = configReader.getBoolean("gateway.session.isVerifyAllMethod", true);
115 if(!isVerifyAllMethod){ 123 if(!isVerifyAllMethod){
@@ -479,4 +487,48 @@ public class SecurityInterceptor implements HandlerInterceptor, ApplicationEvent @@ -479,4 +487,48 @@ public class SecurityInterceptor implements HandlerInterceptor, ApplicationEvent
479 return false; 487 return false;
480 } 488 }
481 } 489 }
  490 +
  491 + /**
  492 + * 是否内网ip
  493 + * @param request
  494 + * @return
  495 + */
  496 + public boolean isInnerIp(HttpServletRequest request){
  497 + //增加内网ip验证开关,供压测时使用
  498 + boolean isInnerIpVerifyEnable = configReader.getBoolean("gateway.security.isInnerIpVerifyEnable", true);
  499 + if(!isInnerIpVerifyEnable){
  500 + return true;
  501 + }
  502 + String ip = getRemoteIP(request);
  503 + String[] ipArr = ip.split(",");
  504 + InetAddress inetAddress = null;
  505 + try {
  506 + inetAddress = InetAddress.getByName( ipArr[ ipArr.length - 1 ].trim() );
  507 + } catch (UnknownHostException e) {
  508 + logger.warn("isInnerIp error is {}", e);
  509 + }
  510 + if ( inetAddress.isSiteLocalAddress() ) {
  511 + // 是内网IP
  512 + return true;
  513 + } else {
  514 + // 不是内网接口
  515 + logger.info( "handler inner api interceptor, {} can not run inner api.", ip );
  516 + return false;
  517 + }
  518 + }
  519 +
  520 +
  521 + /**
  522 + * 获取用户IP
  523 + *
  524 + * @param httpServletRequest 1) x-forwarded-for 2).getRemoteAddr()
  525 + * @return 用户IP
  526 + */
  527 + private String getRemoteIP(final HttpServletRequest httpServletRequest) {
  528 + String ip = httpServletRequest.getHeader("X-Forwarded-For");
  529 + if (StringUtils.isEmpty(ip)) {
  530 + ip = httpServletRequest.getRemoteAddr();
  531 + }
  532 + return ip;
  533 + }
482 } 534 }
@@ -75,6 +75,14 @@ public class SignatureVerifyInterceptor implements HandlerInterceptor, Applicati @@ -75,6 +75,14 @@ public class SignatureVerifyInterceptor implements HandlerInterceptor, Applicati
75 return true; 75 return true;
76 } 76 }
77 77
  78 + //(4) 预留后门, 如果是内网并且传入_sncp参数, 并且验证成功, 不验证.
  79 + String temporaryValueQeq = params.get("_sncp"); //请求的预留后门的值
  80 + String temporaryValue = configReader.getString("gateway.client.secret.h5", "");
  81 + logger.debug("SignatureVerifyInterceptor. method is {}, temporaryValueQeq is {}, temporaryValue is {} and isInner is {}", method, temporaryValueQeq, temporaryValue);
  82 + if (!StringUtils.isEmpty(temporaryValueQeq) && !StringUtils.isEmpty(temporaryValue) && temporaryValueQeq.trim().equals(temporaryValue.trim()) && isInnerIp(httpServletRequest)){
  83 + return true;
  84 + }
  85 +
78 //(4) 验证消息签名. 86 //(4) 验证消息签名.
79 String message = this.getPramsString(params); 87 String message = this.getPramsString(params);
80 String verifyMessage = httpServletRequest.getHeader(HTTP_HEADER_VERIFY_DATA); 88 String verifyMessage = httpServletRequest.getHeader(HTTP_HEADER_VERIFY_DATA);
@@ -188,6 +196,11 @@ public class SignatureVerifyInterceptor implements HandlerInterceptor, Applicati @@ -188,6 +196,11 @@ public class SignatureVerifyInterceptor implements HandlerInterceptor, Applicati
188 * @return 196 * @return
189 */ 197 */
190 public boolean isInnerIp(HttpServletRequest request){ 198 public boolean isInnerIp(HttpServletRequest request){
  199 + //增加内网ip验证开关,供压测时使用
  200 + boolean isInnerIpVerifyEnable = configReader.getBoolean("gateway.security.isInnerIpVerifyEnable", true);
  201 + if(!isInnerIpVerifyEnable){
  202 + return true;
  203 + }
191 String ip = getRemoteIP( request ); 204 String ip = getRemoteIP( request );
192 String[] ipArr = ip.split( "," ); 205 String[] ipArr = ip.split( "," );
193 InetAddress inetAddress = null; 206 InetAddress inetAddress = null;