外观
请求数据加密拦截器 🔐
功能介绍 💡
WARNING
请求数据加密拦截器就像一个解密专家,负责对加密的请求数据进行解密处理。在安全要求较高的系统中,它通过对请求参数进行加密传输来提升系统的整体安全性,有效防止数据在传输过程中被窃取或篡改。
支持的请求格式 📝
- ✅
application/json
- JSON数据 - ✅
application/x-www-form-urlencoded
- 表单数据 - ✅
url地址
- URL参数传递 - ❌
multipart/form-data
- 不支持附件表单数据提交
核心特性
- 🔒 支持多种高安全性加密算法:
3DES
、SM4
- 📦 加密数据统一通过
data
参数传输
加密算法说明 🔒
3DES加密
- 加密方式:3DES(Triple DES)加密算法
- 加密模式:CBC(Cipher Block Chaining)模式
- 填充方式:PKCS5Padding
- 密钥长度:24字节(192位)
- IV处理:随机生成IV,并在密文第一个字节存储IV长度,后面跟着IV和实际密文
- 加密流程:对象 → JSON字符串 → 3DES加密
- 传输要求:加密后的数据必须使用
data
参数名传输
SM4加密
- 加密方式:SM4(国密算法)加密算法
- 加密模式:CBC(Cipher Block Chaining)模式
- 填充方式:PKCS5Padding
- 密钥长度:16字节(128位)
- IV处理:随机生成IV,并在密文第一个字节存储IV长度,后面跟着IV和实际密文
- 加密流程:对象 → JSON字符串 → SM4加密
- 传输要求:加密后的数据必须使用
data
参数名传输
加解密示例
go
// 加密示例
plaintext := []byte(`{"username":"admin","password":"123456"}`)
key := []byte("a748d0d6a748d0d6a748d0d6") // 24字节密钥
ciphertext, err := Encrypt3DES(plaintext, key)
// ciphertext格式:[IV长度(1字节)][IV(8字节)][密文]
// 解密示例
plaintext, err := Decrypt3DES(ciphertext, key)
// plaintext: {"username":"admin","password":"123456"}
详细配置说明 ⚙️
核心参数说明
参数名 | 类型 | 必填 | 默认值 | 说明 |
---|---|---|---|---|
enabled | boolean | 否 | false | 是否启用拦截器 |
rules | array[object] | 是 | - | 加密规则配置列表 |
加密规则配置详解 (rules)
参数名 | 类型 | 必填 | 说明 | 示例值 |
---|---|---|---|---|
urls | array[string] | 是 | 需要进行加密的URL地址列表 | /demo/login |
encrypt-type | string | 否 | 加密算法类型,支持DES3 、SM4 | DES3 |
secret | string | 是 | 数据加密密钥(DES3算法要求24位,SM4算法要求16位) | a748d0d6a748d0d6a748d0d6 |
forbid-urls | array[string] | 否 | 排除不需要加密的URL地址 | /demo/public/** |
type | string | 否 | 验证类型,支持merchant (按商户验证) | merchant |
merchant-secret | map[string][string] | 否 | 商户密钥映射,key为商户ID,value为密钥 | {"1001": "key1", "1002": "key2"} |
配置示例 📋
基础配置
yaml
gateway:
filter:
request-encrypt:
enabled: true # 启用请求加密
rules:
- urls: # 需要加密的地址
- /demo/login
- /demo/get
secret: a748d0d6a748d0d6a748d0d6 # 解密密钥
高级配置
yaml
gateway:
filter:
request-encrypt:
enabled: true # 启用请求加密
rules:
- urls:
- /api/** # 所有API接口
secret: a748d0d6a748d0d6a748d0d6
forbid-urls: # 排除公开接口
- /api/public/**
商户验证配置
yaml
gateway:
filter:
request-encrypt:
enabled: true
rules:
- urls:
- /api/merchant/**
type: merchant # 启用商户验证
merchant-secret: # 商户密钥配置
"1001": "D5E5F589769DA629A2F8C344F4749C0A" # 商户1001的密钥
"1002": "E6F6F690870EB73B3F9D455G5850D1B" # 商户1002的密钥
forbid-urls:
- /api/merchant/public/** # 排除公开接口
完整配置示例
yaml
gateway:
filter:
request-encrypt:
enabled: true
rules:
# 3DES加密 - 用户认证接口
- urls:
- /api/auth/login
- /api/auth/register
- /api/user/password/**
encrypt-type: DES3
secret: a748d0d6a748d0d6a748d0d6 # 24字节密钥
# SM4加密 - 支付相关接口
- urls:
- /api/payment/**
- /api/wallet/**
encrypt-type: SM4
secret: 1234567890abcdef # 16字节密钥
forbid-urls:
- /api/payment/status/** # 排除状态查询接口
# 商户多租户配置
- urls:
- /api/merchant/secure/**
type: merchant
encrypt-type: DES3
merchant-secret:
"1001": "merchant1001key123456789" # 商户1001专用密钥
"1002": "merchant1002key987654321" # 商户1002专用密钥
"1003": "merchant1003keyabcdefghi" # 商户1003专用密钥
高级场景配置
yaml
gateway:
filter:
request-encrypt:
enabled: true
rules:
# 高安全级别 - 管理后台
- urls:
- /api/admin/**
encrypt-type: SM4
secret: admin_secret_key1 # 16字节管理密钥
forbid-urls:
- /api/admin/health/**
- /api/admin/metrics/**
# 中安全级别 - 业务接口
- urls:
- /api/business/**
encrypt-type: DES3
secret: business_key_24_bytes_long # 24字节业务密钥
forbid-urls:
- /api/business/public/**
- /api/business/config/**
# 低安全级别 - 一般接口
- urls:
- /api/common/**
encrypt-type: DES3
secret: common_key_123456789abcdef # 24字节通用密钥
forbid-urls:
- /api/common/info/**
- /api/common/status/**
不同业务场景配置
金融系统
yaml
gateway:
filter:
request-encrypt:
enabled: true
rules:
# 交易接口 - 最高安全级别
- urls:
- /api/transaction/**
- /api/transfer/**
encrypt-type: SM4
secret: finance_sm4_key_1 # 16字节金融密钥
# 账户管理 - 高安全级别
- urls:
- /api/account/**
encrypt-type: DES3
secret: account_des3_key_24_bytes # 24字节账户密钥
forbid-urls:
- /api/account/balance/** # 余额查询不加密
# 银行接口 - 多机构配置
- urls:
- /api/bank/**
type: merchant
encrypt-type: SM4
merchant-secret:
"ICBC": "icbc_secure_key_16b" # 工商银行密钥
"CCB": "ccb_secure_key_16by" # 建设银行密钥
"ABC": "abc_secure_key_16bt" # 农业银行密钥
电商平台
yaml
gateway:
filter:
request-encrypt:
enabled: true
rules:
# 用户敏感信息
- urls:
- /api/user/profile/**
- /api/user/address/**
encrypt-type: DES3
secret: user_profile_key_24_bytes_
# 订单支付信息
- urls:
- /api/order/payment/**
- /api/order/checkout/**
encrypt-type: SM4
secret: payment_sm4_key_1
# 商家接口
- urls:
- /api/seller/**
type: merchant
encrypt-type: DES3
merchant-secret:
"seller001": "seller001_key_24_bytes_abc"
"seller002": "seller002_key_24_bytes_def"
"seller003": "seller003_key_24_bytes_ghi"
forbid-urls:
- /api/seller/products/** # 商品信息不加密
医疗系统
yaml
gateway:
filter:
request-encrypt:
enabled: true
rules:
# 患者隐私信息 - 最高安全级别
- urls:
- /api/patient/**
- /api/medical-record/**
encrypt-type: SM4
secret: medical_sm4_key_1 # 16字节医疗密钥
# 医生操作接口
- urls:
- /api/doctor/diagnosis/**
- /api/doctor/prescription/**
encrypt-type: DES3
secret: doctor_operation_key_24_b
# 医院多机构配置
- urls:
- /api/hospital/**
type: merchant
encrypt-type: SM4
merchant-secret:
"H001": "hospital001_key_16" # 医院001密钥
"H002": "hospital002_key_16" # 医院002密钥
"H003": "hospital003_key_16" # 医院003密钥
forbid-urls:
- /api/hospital/info/** # 医院基本信息不加密
教育平台
yaml
gateway:
filter:
request-encrypt:
enabled: true
rules:
# 学生个人信息
- urls:
- /api/student/profile/**
- /api/student/grade/**
encrypt-type: DES3
secret: student_info_key_24_bytes
# 考试系统
- urls:
- /api/exam/submit/**
- /api/exam/answer/**
encrypt-type: SM4
secret: exam_system_key_1
# 学校多租户配置
- urls:
- /api/school/**
type: merchant
encrypt-type: DES3
merchant-secret:
"SCH001": "school001_key_24_bytes_abc"
"SCH002": "school002_key_24_bytes_def"
"SCH003": "school003_key_24_bytes_ghi"
forbid-urls:
- /api/school/courses/** # 课程信息不加密
商户验证说明
- 当type设置为merchant时,系统会根据请求中的商户ID选择对应的密钥
- 商户ID必须通过请求头传递
X-Merchant-Id
- 如果找不到对应的商户密钥,将使用默认密钥
- 建议为每个商户配置独立的密钥
- 建议结合签名使用
注意事项
商户密钥配置
- 确保每个商户的密钥都是唯一的
- 定期更新商户密钥
- 妥善保管密钥信息
- 避免在日志中打印密钥
安全建议
- 使用HTTPS传输
- 实现密钥定期轮换机制
- 记录密钥使用日志
- 设置密钥有效期
请求示例 📝
基础请求示例
加密前的数据
json
{
"username": "admin",
"password": "123456"
}
加密后的请求
http
POST /demo/login HTTP/1.1
Content-Type: application/json
{
"data": "U2FsdGVkX1+2tOLqT8HqVJ6Y+6P8v+3/RjMYn+5p+8Q="
}
商户验证请求示例
请求头
http
POST /api/merchant/login HTTP/1.1
Content-Type: application/json
X-Merchant-Id: 1001 # 商户ID
请求体
json
{
"data": "U2FsdGVkX1+2tOLqT8HqVJ6Y+6P8v+3/RjMYn+5p+8Q="
}
最佳实践 💡
密钥管理
- 使用足够长度和复杂度的密钥
- 不同环境使用不同的密钥
- 定期更换密钥
安全建议
- 使用HTTPS传输
- 配合签名验证使用
- 对敏感接口启用加密
性能优化
- 只对必要的接口启用加密
- 合理设置排除规则
- 避免对大文件传输使用加密
常见问题 ❓
解密失败
- 检查密钥是否正确
- 验证数据是否使用
data
参数传输 - 确认加密算法是否匹配
配置不生效
- 确认
enabled
是否为 true - 检查 URL 是否在配置列表中
- 验证请求格式是否支持
- 确认
性能问题
- 检查是否对大量数据进行加密
- 确认是否对高频接口启用了加密
- 优化加密范围
安全提示 ⚠️
CAUTION
- 密钥泄露会导致安全风险,请妥善保管
- 建议结合其他安全措施(如签名验证)一起使用
- 定期更新密钥,提高系统安全性
- 避免在日志中打印解密后的敏感数据