Repository: huaweicloud/sermant
Issue: 【fix】fix bug for springboot registry plugin

### What happened?

if the url which for springboot registry plugin is invalid, it will throw ex

### How can we reproduce it (as minimally and precisely as possible)?

use url request whose url is invalid, such as http://www.domain.com/test

### Anything else we need to know?

_No response_

### Sermant version

<details>
0.7.0
</details>


### OS version

<details>

```console
# On Linux:
$ cat /etc/os-release
# paste output here
$ uname -a
# paste output here

# On Windows:
C:\\> wmic os get Caption, Version, BuildNumber, OSArchitecture
# paste output here
```
</details>


Changed files:
sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/httpclient/HttpAsyncClient4xInterceptor.java
sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/utils/RequestInterceptorUtils.java
sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/httpclient/HttpClient4xInterceptor.java
sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/httpconnection/HttpUrlConnectionConnectInterceptor.java
sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/RestTemplateInterceptor.java
sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/FeignInvokeInterceptor.java
sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttpClientInterceptor.java
sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttp3ClientInterceptor.java
sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/utils/PlugEffectWhiteBlackUtils.java

diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/FeignInvokeInterceptor.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/FeignInvokeInterceptor.java
index c48a5913..e86e3bc3 100644
--- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/FeignInvokeInterceptor.java
+++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/FeignInvokeInterceptor.java
@@ -48,8 +48,7 @@ public class FeignInvokeInterceptor extends MarkInterceptor {
         final InvokerService invokerService = PluginServiceManager.getPluginService(InvokerService.class);
         Request request = (Request) context.getArguments()[0];
         Map<String, String> urlInfo = RequestInterceptorUtils.recoverUrl(request.url());
-        if (!PlugEffectWhiteBlackUtils.isAllowRun(request.url(), urlInfo.get(HttpConstants.HTTP_URI_HOST),
-            false)) {
+        if (!PlugEffectWhiteBlackUtils.isAllowRun(request.url(), urlInfo.get(HttpConstants.HTTP_URI_HOST))) {
             return context;
         }
         RequestInterceptorUtils.printRequestLog("feign", urlInfo);
diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttp3ClientInterceptor.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttp3ClientInterceptor.java
index 062f1f4a..daca8098 100644
--- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttp3ClientInterceptor.java
+++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttp3ClientInterceptor.java
@@ -64,9 +64,11 @@ public class OkHttp3ClientInterceptor extends MarkInterceptor {
         }
         Request request = rawRequest.get();
         URI uri = request.url().uri();
+        if (!PlugEffectWhiteBlackUtils.isHostEqualRealmName(uri.getHost())) {
+            return context;
+        }
         Map<String, String> hostAndPath = RequestInterceptorUtils.recoverHostAndPath(uri.getPath());
-        if (!PlugEffectWhiteBlackUtils.isAllowRun(uri.getHost(), hostAndPath.get(HttpConstants.HTTP_URI_HOST),
-            true)) {
+        if (!PlugEffectWhiteBlackUtils.isPlugEffect(hostAndPath.get(HttpConstants.HTTP_URI_HOST))) {
             return context;
         }
         RequestInterceptorUtils.printRequestLog("OkHttp3", hostAndPath);
diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttpClientInterceptor.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttpClientInterceptor.java
index 8e72882f..6070583c 100644
--- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttpClientInterceptor.java
+++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/OkHttpClientInterceptor.java
@@ -66,9 +66,11 @@ public class OkHttpClientInterceptor extends MarkInterceptor {
         }
         Request request = rawRequest.get();
         URI uri = request.uri();
+        if (!PlugEffectWhiteBlackUtils.isHostEqualRealmName(uri.getHost())) {
+            return context;
+        }
         Map<String, String> hostAndPath = RequestInterceptorUtils.recoverHostAndPath(uri.getPath());
-        if (!PlugEffectWhiteBlackUtils.isAllowRun(uri.getHost(), hostAndPath.get(HttpConstants.HTTP_URI_HOST),
-            true)) {
+        if (!PlugEffectWhiteBlackUtils.isPlugEffect(hostAndPath.get(HttpConstants.HTTP_URI_HOST))) {
             return context;
         }
         RequestInterceptorUtils.printRequestLog("OkHttp", hostAndPath);
diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/RestTemplateInterceptor.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/RestTemplateInterceptor.java
index 62181676..73dcf258 100644
--- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/RestTemplateInterceptor.java
+++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/RestTemplateInterceptor.java
@@ -50,9 +50,11 @@ public class RestTemplateInterceptor extends MarkInterceptor {
         final InvokerService invokerService = PluginServiceManager.getPluginService(InvokerService.class);
         URI uri = (URI) context.getArguments()[0];
         HttpMethod httpMethod = (HttpMethod) context.getArguments()[1];
+        if (!PlugEffectWhiteBlackUtils.isHostEqualRealmName(uri.getHost())) {
+            return context;
+        }
         Map<String, String> hostAndPath = RequestInterceptorUtils.recoverHostAndPath(uri.getPath());
-        if (!PlugEffectWhiteBlackUtils.isAllowRun(uri.getHost(), hostAndPath.get(HttpConstants.HTTP_URI_HOST),
-            true)) {
+        if (!PlugEffectWhiteBlackUtils.isPlugEffect(hostAndPath.get(HttpConstants.HTTP_URI_HOST))) {
             return context;
         }
         RequestInterceptorUtils.printRequestLog("restTemplate", hostAndPath);
diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/httpclient/HttpAsyncClient4xInterceptor.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/httpclient/HttpAsyncClient4xInterceptor.java
index c569196f..d8d525dd 100644
--- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/httpclient/HttpAsyncClient4xInterceptor.java
+++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/httpclient/HttpAsyncClient4xInterceptor.java
@@ -80,7 +80,11 @@ public class HttpAsyncClient4xInterceptor implements Interceptor {
     @Override
     public ExecuteContext before(ExecuteContext context) throws Exception {
         ready();
-        acquireHostPath(context);
+        HttpAsyncRequestProducer httpAsyncRequestProducer = (HttpAsyncRequestProducer) context.getArguments()[0];
+        if (!PlugEffectWhiteBlackUtils.isHostEqualRealmName(httpAsyncRequestProducer.getTarget().getHostName())) {
+            return context;
+        }
+        acquireHostPath(httpAsyncRequestProducer);
         if (!isConfigEnable()) {
             // 配置不允许则直接返回
             return context;
@@ -99,6 +103,7 @@ public class HttpAsyncClient4xInterceptor implements Interceptor {
     @Override
     public ExecuteContext after(ExecuteContext context) throws Exception {
         if (!isConfigEnable()) {
+            HttpAsyncUtils.remove();
             return context;
         }
         final ClassLoader appClassloader = Thread.currentThread().getContextClassLoader();
@@ -134,12 +139,10 @@ public class HttpAsyncClient4xInterceptor implements Interceptor {
         if (originHostName == null || hostAndPath == null) {
             return false;
         }
-        return PlugEffectWhiteBlackUtils
-                .isAllowRun(originHostName, hostAndPath.get(HttpConstants.HTTP_URI_HOST), true);
+        return PlugEffectWhiteBlackUtils.isAllowRun(originHostName, hostAndPath.get(HttpConstants.HTTP_URI_HOST));
     }
 
-    private void acquireHostPath(ExecuteContext context) throws Exception {
-        HttpAsyncRequestProducer httpAsyncRequestProducer = (HttpAsyncRequestProducer) context.getArguments()[0];
+    private void acquireHostPath(HttpAsyncRequestProducer httpAsyncRequestProducer) throws Exception {
         final HttpHost httpHost = httpAsyncRequestProducer.getTarget();
         final HttpRequest httpRequest = httpAsyncRequestProducer.generateRequest();
         final Optional<URI> optionalUri = RequestInterceptorUtils.formatUri(httpRequest.getRequestLine().getUri());
diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/httpclient/HttpClient4xInterceptor.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/httpclient/HttpClient4xInterceptor.java
index f6b2c886..ac970729 100644
--- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/httpclient/HttpClient4xInterceptor.java
+++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/httpclient/HttpClient4xInterceptor.java
@@ -67,12 +67,15 @@ public class HttpClient4xInterceptor extends MarkInterceptor {
         final InvokerService invokerService = PluginServiceManager.getPluginService(InvokerService.class);
         HttpHost httpHost = (HttpHost) context.getArguments()[0];
         final HttpRequest httpRequest = (HttpRequest) context.getArguments()[1];
+        if (!PlugEffectWhiteBlackUtils.isHostEqualRealmName(httpHost.getHostName())) {
+            return context;
+        }
         final Map<String, String> hostAndPath = RequestInterceptorUtils.recoverUrl(httpRequest.getRequestLine()
                 .getUri());
         if (hostAndPath.isEmpty()) {
             return context;
         }
-        if (!isConfigEnable(hostAndPath, httpHost.getHostName())) {
+        if (!PlugEffectWhiteBlackUtils.isPlugEffect(hostAndPath.get(HttpConstants.HTTP_URI_HOST))) {
             return context;
         }
         RequestInterceptorUtils.printRequestLog("HttpClient", hostAndPath);
@@ -154,10 +157,6 @@ public class HttpClient4xInterceptor extends MarkInterceptor {
         };
     }
 
-    private boolean isConfigEnable(Map<String, String> hostAndPath, String hostName) {
-        return PlugEffectWhiteBlackUtils.isAllowRun(hostName, hostAndPath.get(HttpConstants.HTTP_URI_HOST), true);
-    }
-
     @Override
     protected void ready() {
         if (isLoaded.compareAndSet(false, true)) {
diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/httpconnection/HttpUrlConnectionConnectInterceptor.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/httpconnection/HttpUrlConnectionConnectInterceptor.java
index 7ef28948..a4faf8ec 100644
--- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/httpconnection/HttpUrlConnectionConnectInterceptor.java
+++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/httpconnection/HttpUrlConnectionConnectInterceptor.java
@@ -81,10 +81,12 @@ public class HttpUrlConnectionConnectInterceptor extends MarkInterceptor {
             return context;
         }
         final URL url = rawUrl.get();
+        if (!PlugEffectWhiteBlackUtils.isHostEqualRealmName(url.getHost())) {
+            return context;
+        }
         final String fullUrl = url.toString();
         Map<String, String> urlInfo = RequestInterceptorUtils.recoverUrl(url);
-        if (!PlugEffectWhiteBlackUtils.isAllowRun(url.getHost(), urlInfo.get(HttpConstants.HTTP_URI_HOST),
-            false)) {
+        if (!PlugEffectWhiteBlackUtils.isPlugEffect(urlInfo.get(HttpConstants.HTTP_URI_HOST))) {
             return context;
         }
         HttpConnectionUtils.save(new HttpConnectionContext(urlInfo, url));
diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/utils/PlugEffectWhiteBlackUtils.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/utils/PlugEffectWhiteBlackUtils.java
index b6396969..21630568 100644
--- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/utils/PlugEffectWhiteBlackUtils.java
+++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/utils/PlugEffectWhiteBlackUtils.java
@@ -135,14 +135,10 @@ public class PlugEffectWhiteBlackUtils {
      *
      * @param realmStr 拦截获取的域名
      * @param serviceName 下游服务名
-     * @param isByEqual 是否基于等于比较
      * @return 是否允许执行
      */
-    public static boolean isAllowRun(String realmStr, String serviceName, boolean isByEqual) {
-        if (isByEqual && !PlugEffectWhiteBlackUtils.isHostEqualRealmName(realmStr)) {
-            return false;
-        }
-        if (!isByEqual && !PlugEffectWhiteBlackUtils.isUrlContainsRealmName(realmStr)) {
+    public static boolean isAllowRun(String realmStr, String serviceName) {
+        if (!PlugEffectWhiteBlackUtils.isHostEqualRealmName(realmStr)) {
             return false;
         }
         return PlugEffectWhiteBlackUtils.isPlugEffect(serviceName);
diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/utils/RequestInterceptorUtils.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/utils/RequestInterceptorUtils.java
index acfa9abe..885d17c5 100644
--- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/utils/RequestInterceptorUtils.java
+++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/utils/RequestInterceptorUtils.java
@@ -55,6 +55,17 @@ public class RequestInterceptorUtils {
 
     private static final int URL_INFO_INIT_SIZE = 8;
 
+    /**
+     * 解析url时，分隔的最小长度, 其path必须为/serviceName/api/xxx, 否则将不是目标请求 {@link RequestInterceptorUtils#recoverUrl(URL)}
+     */
+    private static final int MIN_LEN_FOR_VALID_PATH = 2;
+
+    /**
+     * 解析url时，分隔的最小长度, 其url必须为http:/www.domain.com/serviceName/api/xxx, 否则将不是目标请求
+     * {@link RequestInterceptorUtils#recoverUrl(String)}
+     */
+    private static final int MIN_LEN_FOR_VALID_URL = 4;
+
     private RequestInterceptorUtils() {
     }
 
@@ -75,14 +86,17 @@ public class RequestInterceptorUtils {
         if (url == null) {
             return Collections.emptyMap();
         }
-        final Map<String, String> result = new HashMap<>(URL_INFO_INIT_SIZE);
         final String protocol = url.getProtocol();
 
         // /serviceName/sayHello?name=1, 已切分为serviceName, sayHello?name=1
-        final StringTokenizer tokenizer = new StringTokenizer(url.getPath());
+        String delim = String.valueOf(HttpConstants.HTTP_URL_SINGLE_SLASH);
+        final StringTokenizer tokenizer = new StringTokenizer(url.getPath(), delim);
+        if (tokenizer.countTokens() < MIN_LEN_FOR_VALID_PATH) {
+            return Collections.emptyMap();
+        }
+        final Map<String, String> result = new HashMap<>(URL_INFO_INIT_SIZE);
         result.put(HttpConstants.HTTP_URL_SCHEME, protocol);
-        result.put(HttpConstants.HTTP_URI_HOST,
-                tokenizer.nextToken(String.valueOf(HttpConstants.HTTP_URL_SINGLE_SLASH)));
+        result.put(HttpConstants.HTTP_URI_HOST, tokenizer.nextToken(delim));
         result.put(HttpConstants.HTTP_URI_PATH,
                 formatPath(tokenizer.nextToken(HttpConstants.EMPTY_STR), url.getQuery()));
         return result;
@@ -98,8 +112,11 @@ public class RequestInterceptorUtils {
         if (StringUtils.isEmpty(url) || !isValidUrl(url)) {
             return Collections.emptyMap();
         }
-        final StringTokenizer urlTokens = new StringTokenizer(url);
         String baseSlash = String.valueOf(HttpConstants.HTTP_URL_SINGLE_SLASH);
+        final StringTokenizer urlTokens = new StringTokenizer(url, baseSlash);
+        if (urlTokens.countTokens() < MIN_LEN_FOR_VALID_URL) {
+            return Collections.emptyMap();
+        }
 
         // http(s):.subString(0, len - 1)
         final String rawScheme = urlTokens.nextToken(baseSlash);