快速网关设置自定义策略路径通配符



我创建了一个基本插件,可以执行一些自定义的 Jwt 验证,并在策略中的 req 上设置一个"user"对象。

它不是很相关,但中间件 plicy 看起来像这样:

const { getJwtFromCookies, getJwtFromHeader } = require("../lib/jwt");
const { verifyToken } = require("../lib/jwt");

module.exports = {
name: 'jwt-policy',
policy: (actionParams) => {
return (req, res, next) => {
console.log('test plugin');
const jwt = getJwtFromCookies(req) || getJwtFromHeader(req);
try {
req.user = verifyToken(jwt);
console.log('plugin:', req.user);
} catch (e) {
res.send(401);
}
next() // calling next policy
};
}
};

在 api 网关中,当我不指定路径时,会正确调用插件。
但是当我用这样的通配符放置路径时,它不会被调用(我不想在所有路由上调用插件,也不想要一个"精确"路径:

user:
apiEndpoints:
- user
policies:
- jwt-policy:
condition: # this action is executed only if path is exactly /v1/auth
name: pathExact
path: '/v1/auth/*'

正确的声明是什么/我在文档中的什么地方可以找到它?

从源代码中我看到所需的格式是:

- jwt-policy:
condition: 
name: pathMatch
pattern: /v1/auth/*

我仍在处理插件中的 req 对象上设置的属性不会传播到实际端点的问题。这对我的情况来说仍然是一个问题。

编辑:通过使用插件中的 egContext 属性修复的第二个问题:

req.egContext.authUser = verifyToken(jwt);

并定义请求转换器:

- request-transformer:
- action:
body:
add:
authUser: req.egContext.authUser

将此属性添加到请求正文而不是立即添加到 req 对象不是很可取,但我认为快速网关目前不支持此功能。我为此打开了一个功能请求。

最新更新