fix: dynamic routing using cookies

pull/1097/head
jealhuang 2 years ago
parent 3bb486befb
commit f1e71da689

@ -149,7 +149,7 @@ public class FeignExpressionLabelUtilsTest {
requestTemplate.method(Request.HttpMethod.GET);
requestTemplate.target("http://localhost");
requestTemplate = requestTemplate.resolve(new HashMap<>());
requestTemplate.header("cookie", Collections.singleton("uid=zhangsan"));
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)

@ -184,18 +184,14 @@ public final class ExpressionLabelUtils {
}
for (String value : values) {
String[] split = value.split("=");
if (split.length == 0) {
return StringUtils.EMPTY;
}
if (StringUtils.isEmpty(split[0]) || StringUtils.isEmpty(split[1])) {
return StringUtils.EMPTY;
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];
}
}
if (key.equals(split[0])) {
return split[1];
}
}
return StringUtils.EMPTY;
}
}

@ -18,11 +18,14 @@
package com.tencent.cloud.common.util.expresstion;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import io.netty.handler.codec.http.HttpHeaderNames;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpCookie;
@ -110,6 +113,13 @@ public final class SpringWebExpressionLabelUtils {
}
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)) {
labels.put(labelKey, request.getMethodValue());
}
@ -158,4 +168,19 @@ public final class SpringWebExpressionLabelUtils {
String query = request.getURI().getQuery();
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.setURI(URI.create("http://calleeService/user/get?uid=zhangsan"));
request.getHeaders().add("uid", "zhangsan");
request.getHeaders().add("cookie", "uid=zhangsan; auth-token=hauigdfu8esgf8");
Map<String, String> result = SpringWebExpressionLabelUtils.resolve(request, labelKeys);
assertThat(result.get(validLabel1)).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(validLabel5)).isEqualTo("/user/get");
assertThat(result.get(invalidLabel1)).isNull();

Loading…
Cancel
Save