@polyv/request-plugin-aes-decrypt
v2.8.1
Published
用途:AES 解密插件,用于对服务端返回的加密数据进行解密处理。
Readme
AESDecryptRequestPlugin
用途:AES 解密插件,用于对服务端返回的加密数据进行解密处理。
实例化参数
| 参数名 | 用途 | 类型 | 默认值 |
| ----------------- | ------------------ | -------------------- | --------------------------------- |
| decryptKeys | 解密密钥信息 | AESKeyInfo | - |
| decryptPosition | 解密位置 | AESDecryptPosition | AESDecryptPosition.ResponseData |
| getDecryptInfo | 获取解密信息的方法 | function | - |
请求选项
| 参数名 | 用途 | 类型 | 默认值 |
| ---------------- | ----------------- | ---------------- | ------- |
| useAESDecrypt | 是否使用 AES 解密 | boolean | false |
| aesDecryptInfo | AES 解密信息 | AESDecryptInfo | - |
解密密钥信息类型
AESKeyInfo 类型如下:
| 参数名 | 用途 | 类型 | 默认值 |
| ------ | ---------------- | -------- | ------ |
| sign | 解密签名(密钥) | string | - |
| iv | 解密偏移量 | string | - |
解密位置枚举
AESDecryptPosition 枚举如下:
| 枚举值 | 描述 |
| ---------------------------------- | ------------------------ |
| AESDecryptPosition.WholeResponse | 解密整个响应数据 |
| AESDecryptPosition.ResponseData | 仅解密响应中的 data 字段 |
使用方式
import { PolyvRequest } from '@polyv/request-core';
import { AESDecryptRequestPlugin, AESDecryptPosition } from '@polyv/request-plugin-aes-decrypt';
// 基础用法:使用固定的解密密钥
const requester = new PolyvRequest({
requestPlugins: [
new AESDecryptRequestPlugin({
decryptKeys: {
sign: 'yourDecryptKey',
iv: 'yourDecryptIV',
},
}),
],
});
// 配置解密位置
const requester = new PolyvRequest({
requestPlugins: [
new AESDecryptRequestPlugin({
decryptKeys: {
sign: 'yourDecryptKey',
iv: 'yourDecryptIV',
},
decryptPosition: AESDecryptPosition.WholeResponse,
}),
],
});
// 使用 getDecryptInfo 方法动态获取解密信息
const requester = new PolyvRequest({
requestPlugins: [
new AESDecryptRequestPlugin({
getDecryptInfo: async options => {
// 可以根据 options 中的信息动态获取解密密钥
return {
decryptKeys: {
sign: 'dynamicDecryptKey',
iv: 'dynamicDecryptIV',
},
decryptPosition: AESDecryptPosition.ResponseData,
};
},
}),
],
});
// 在某个请求中启用 AES 解密
requester.get(
'/api/data',
{},
{
useAESDecrypt: true,
},
);
// 在某个请求中使用特定的解密信息
requester.get(
'/api/data',
{},
{
useAESDecrypt: true,
aesDecryptInfo: {
decryptKeys: {
sign: 'specificKey',
iv: 'specificIV',
},
decryptPosition: AESDecryptPosition.WholeResponse,
},
},
);解密处理逻辑
- 插件默认不启用,需要在请求选项中设置
useAESDecrypt: true或全局配置启用 - 解密信息的优先级为:请求选项中的
aesDecryptInfo>getDecryptInfo方法返回的信息 > 初始化时提供的配置 - 根据
decryptPosition的设置,可以选择解密整个响应数据或只解密响应中的 data 字段 - 解密后,如果解密结果是有效的 JSON 字符串,会自动解析为 JavaScript 对象
