fix: dynamic routing using cookies. (#1153)

pull/1155/head
Blankjee 1 year ago committed by GitHub
parent 947c0d8fb0
commit d1e4a74ac4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -8,3 +8,4 @@
- [feat:support service contract reporting.](https://github.com/Tencent/spring-cloud-tencent/pull/1141) - [feat:support service contract reporting.](https://github.com/Tencent/spring-cloud-tencent/pull/1141)
- [feat: support log path configuration parameters,](https://github.com/Tencent/spring-cloud-tencent/pull/1142) - [feat: support log path configuration parameters,](https://github.com/Tencent/spring-cloud-tencent/pull/1142)
- [feat:add swagger report switch.](https://github.com/Tencent/spring-cloud-tencent/pull/1150) - [feat:add swagger report switch.](https://github.com/Tencent/spring-cloud-tencent/pull/1150)
- [fix: dynamic routing using cookies.](https://github.com/Tencent/spring-cloud-tencent/pull/1153)

@ -63,6 +63,13 @@ public final class FeignExpressionLabelUtils {
} }
labels.put(labelKey, getQueryValue(request, queryKey)); labels.put(labelKey, getQueryValue(request, queryKey));
} }
else if (ExpressionLabelUtils.isCookieLabel(labelKey)) {
String cookieKey = ExpressionLabelUtils.parseCookieKey(labelKey);
if (StringUtils.isBlank(cookieKey)) {
continue;
}
labels.put(labelKey, getCookieValue(request, cookieKey));
}
else if (ExpressionLabelUtils.isMethodLabel(labelKey)) { else if (ExpressionLabelUtils.isMethodLabel(labelKey)) {
labels.put(labelKey, request.method()); labels.put(labelKey, request.method());
} }
@ -84,4 +91,9 @@ public final class FeignExpressionLabelUtils {
public static String getQueryValue(RequestTemplate request, String key) { public static String getQueryValue(RequestTemplate request, String key) {
return ExpressionLabelUtils.getFirstValue(request.queries(), key); return ExpressionLabelUtils.getFirstValue(request.queries(), key);
} }
public static String getCookieValue(RequestTemplate request, String key) {
Map<String, Collection<String>> headers = request.headers();
return ExpressionLabelUtils.getCookieFirstValue(headers, key);
}
} }

@ -18,6 +18,7 @@
package com.tencent.cloud.polaris.router.feign; package com.tencent.cloud.polaris.router.feign;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -137,4 +138,24 @@ public class FeignExpressionLabelUtilsTest {
assertThat(result).isNotEmpty(); assertThat(result).isNotEmpty();
assertThat(result.get(labelKey1)).isEqualTo(uri); assertThat(result.get(labelKey1)).isEqualTo(uri);
} }
@Test
public void testGetCookie() {
String uri = "/";
String cookieValue = "zhangsan";
RequestTemplate requestTemplate = new RequestTemplate();
requestTemplate.uri(uri);
requestTemplate.method(Request.HttpMethod.GET);
requestTemplate.target("http://localhost");
requestTemplate = requestTemplate.resolve(new HashMap<>());
requestTemplate.header("cookie", Collections.singleton("uid=zhangsan; auth-token=dfhuwshfy77"));
String labelKey1 = "${http.cookie.uid}";
Map<String, String> result = FeignExpressionLabelUtils.resolve(requestTemplate, Stream.of(labelKey1)
.collect(toSet()));
assertThat(result).isNotEmpty();
assertThat(result.get(labelKey1)).isEqualTo(cookieValue);
}
} }

@ -23,6 +23,7 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import io.netty.handler.codec.http.HttpHeaderNames;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@ -170,4 +171,27 @@ public final class ExpressionLabelUtils {
return StringUtils.EMPTY; return StringUtils.EMPTY;
} }
public static String getCookieFirstValue(Map<String, Collection<String>> valueMaps, String key) {
if (CollectionUtils.isEmpty(valueMaps)) {
return StringUtils.EMPTY;
}
Collection<String> values = valueMaps.get(HttpHeaderNames.COOKIE.toString());
if (CollectionUtils.isEmpty(values)) {
return StringUtils.EMPTY;
}
for (String value : values) {
String[] cookieArray = StringUtils.split(value, ";");
for (String cookieValue : cookieArray) {
String[] cookieKV = StringUtils.split(cookieValue, "=");
if (cookieKV != null && cookieKV.length == 2 && StringUtils.equals(cookieKV[0], key)) {
return cookieKV[1];
}
}
}
return StringUtils.EMPTY;
}
} }

@ -110,6 +110,13 @@ public final class SpringWebExpressionLabelUtils {
} }
labels.put(labelKey, getQueryValue(request, queryKey)); labels.put(labelKey, getQueryValue(request, queryKey));
} }
else if (ExpressionLabelUtils.isCookieLabel(labelKey)) {
String cookieKey = ExpressionLabelUtils.parseCookieKey(labelKey);
if (StringUtils.isBlank(cookieKey)) {
continue;
}
labels.put(labelKey, getCookieValue(request, cookieKey));
}
else if (ExpressionLabelUtils.isMethodLabel(labelKey)) { else if (ExpressionLabelUtils.isMethodLabel(labelKey)) {
labels.put(labelKey, request.getMethodValue()); labels.put(labelKey, request.getMethodValue());
} }
@ -158,4 +165,19 @@ public final class SpringWebExpressionLabelUtils {
String query = request.getURI().getQuery(); String query = request.getURI().getQuery();
return ExpressionLabelUtils.getQueryValue(query, key); return ExpressionLabelUtils.getQueryValue(query, key);
} }
public static String getCookieValue(HttpRequest request, String key) {
String first = request.getHeaders().getFirst(HttpHeaders.COOKIE);
if (StringUtils.isEmpty(first)) {
return StringUtils.EMPTY;
}
String[] cookieArray = StringUtils.split(first, ";");
for (String cookieItem : cookieArray) {
String[] cookieKv = StringUtils.split(cookieItem, "=");
if (cookieKv != null && cookieKv.length == 2 && StringUtils.equals(cookieKv[0], key)) {
return cookieKv[1];
}
}
return StringUtils.EMPTY;
}
} }

@ -197,12 +197,13 @@ public class ExpressionLabelUtilsTest {
request.setMethod(HttpMethod.GET); request.setMethod(HttpMethod.GET);
request.setURI(URI.create("http://calleeService/user/get?uid=zhangsan")); request.setURI(URI.create("http://calleeService/user/get?uid=zhangsan"));
request.getHeaders().add("uid", "zhangsan"); request.getHeaders().add("uid", "zhangsan");
request.getHeaders().add("cookie", "uid=zhangsan; auth-token=hauigdfu8esgf8");
Map<String, String> result = SpringWebExpressionLabelUtils.resolve(request, labelKeys); Map<String, String> result = SpringWebExpressionLabelUtils.resolve(request, labelKeys);
assertThat(result.get(validLabel1)).isEqualTo("zhangsan"); assertThat(result.get(validLabel1)).isEqualTo("zhangsan");
assertThat(result.get(validLabel2)).isEqualTo("zhangsan"); assertThat(result.get(validLabel2)).isEqualTo("zhangsan");
assertThat(result.get(validLabel3)).isNull(); assertThat(result.get(validLabel3)).isEqualTo("zhangsan");
assertThat(result.get(validLabel4)).isEqualTo("GET"); assertThat(result.get(validLabel4)).isEqualTo("GET");
assertThat(result.get(validLabel5)).isEqualTo("/user/get"); assertThat(result.get(validLabel5)).isEqualTo("/user/get");
assertThat(result.get(invalidLabel1)).isNull(); assertThat(result.get(invalidLabel1)).isNull();

Loading…
Cancel
Save