[frontend-gray] support grayKey from localStorage (#1395)

This commit is contained in:
mamba
2024-10-18 13:58:52 +08:00
committed by GitHub
parent c67f494b49
commit 11ff2d1d31
6 changed files with 115 additions and 43 deletions

View File

@@ -17,6 +17,7 @@ description: 前端灰度插件配置参考
| 名称 | 数据类型 | 填写要求 | 默认值 | 描述 |
|----------------|--------------|----|-----|----------------------------------------------------------------------------------------------------|
| `grayKey` | string | 非必填 | - | 用户ID的唯一标识可以来自Cookie或者Header中比如 userid如果没有填写则使用`rules[].grayTagKey``rules[].grayTagValue`过滤灰度规则 |
| `localStorageGrayKey` | string | 非必填 | - | 使用JWT鉴权方式用户ID的唯一标识来自`localStorage`中,如果配置了当前参数,则`grayKey`失效 |
| `graySubKey` | string | 非必填 | - | 用户身份信息可能以JSON形式透出比如`userInfo:{ userCode:"001" }`,当前例子`graySubKey`取值为`userCode` |
| `userStickyMaxAge` | int | 非必填 | 172800 | 用户粘滞的时长:单位为秒,默认为`172800`2天时间 |
| `rules` | array of object | 必填 | - | 用户定义不同的灰度规则,适配不同的灰度场景 |
@@ -168,6 +169,30 @@ cookie存在`appInfo`的JSON数据其中包含`userId`字段为当前的唯
否则使用`version: base`版本
### 用户信息存储在LocalStorage
由于网关插件需要识别用户为唯一身份信息HTTP协议进行信息传输只能在Header中传递。如果用户信息存储在LocalStorage在首页注入一段脚本将LocalStorage中的用户信息设置到cookie中。
```
(function() {
var grayKey = '@@X_GRAY_KEY';
var cookies = document.cookie.split('; ').filter(function(row) {
return row.indexOf(grayKey + '=') === 0;
});
try {
if (typeof localStorage !== 'undefined' && localStorage !== null) {
var storageValue = localStorage.getItem(grayKey);
var cookieValue = cookies.length > 0 ? decodeURIComponent(cookies[0].split('=')[1]) : null;
if (storageValue && storageValue.indexOf('=') < 0 && cookieValue && cookieValue !== storageValue) {
document.cookie = grayKey + '=' + encodeURIComponent(storageValue) + '; path=/;';
window.location.reload();
}
}
} catch (error) {
// xx
}
})();
```
### rewrite重写配置
> 一般用于CDN部署场景
```yml