express - 在 express-gateway 中运行自定义函数
问题描述
我在 gateway.config.yml (Express-Gateway api)中有这个配置:
- bo
policies:
- jwt:
- action:
secretOrPublicKeyFile: './key.pem'
checkCredentialExistence: false
一切正常,但我希望客户端对正在发送的令牌进行编码/加密,以确保即使我在本地存储中有令牌存储,也没有人可以使用它,因为它需要由客户端签名。
唯一的问题是,如何在 Express-Gateway jwt 策略尝试验证令牌之前运行代码来解码/解密令牌?
因为 express-gateway 可以像任何其他 express 应用程序一样使用中间件,我认为这是可能的,但不知道如何去做。
我创建了这个对我有帮助的策略,但是如何将它与 express-gateway api 集成:
const cryptojs = require("crypto-js");
module.exports = {
name: 'decode',
policy: (actionParams) => {
return (req, res, next) => {
const tokenHeader = req.header('Authorization');
const tokenArray = tokenHeader.split(' ');
const tokenCifer = tokenArray[1];
const bytes = cryptojs.AES.decrypt(tokenCifer, 'superkeyperm'); //CryptoJS.AES.decrypt(ciphertext.toString(), 'secret key 123');
var token = bytes.toString(cryptojs.enc.Utf8);
req.headers.authorization = `Bearer ${token}`;
next() // calling next policy
};
}
};
解决方案
我认为您感兴趣的是编写一个插件,它只不过是您可以在 Express Gateway 中堆叠的附加中间件和条件的集合,您可以在其中放置自己的逻辑。
推荐阅读
- java-8 - Java Apache2 日志文件权限被拒绝
- django - 在 django 中使用 postmant 请求的 302 状态码
- c - 函数删除链表中给定值的所有节点
- r - 迭代查找另一个数据集中具有 x 点距离的所有点
- ember.js - SASS:如何在构建之前替换导入(Ember 项目)
- c# - 如何在 C# 中按位置获取行和列?
- c++ - 以下用例的最佳 C++ 容器
- javascript - 获得可见的刻度/轴范围
- angularjs - 在 Angular 中添加数据表列过滤
- javascript - 使基本 URL 可在整个 React 应用程序中访问(对于 dev 和 prod 不同)