sm2-加密

This commit is contained in:
wu.jifen
2025-08-25 16:57:10 +08:00
parent 8b3a60f22d
commit e97eac836e
4 changed files with 214 additions and 84 deletions

View File

@@ -8,8 +8,7 @@ import { logger } from 'runjs/lib/common'
import {
decryptWithPrivateKey,
encrypt,
shouldEncrypt,
privateKeyHex
shouldEncrypt
} from '@/assets/js/utils/encrypt'
// create an axios instance
@@ -57,12 +56,13 @@ service.interceptors.request.use(
return true
}
// 跳过自定义标记不加密的请求
return !shouldEncrypt(config)
// // 跳过自定义标记不加密的请求 (在encrypt.js中已写这部分逻辑)
const url = config.url.substring(config.url.lastIndexOf(':')).substring(6)
return !shouldEncrypt(url)
}
// 4. 执行加密逻辑
if (!shouldSkipEncryption() && shouldEncrypt(config)) {
if (!shouldSkipEncryption()) {
// 添加加密标识(便于调试)
config.headers['X-Encrypted'] = 'true'
@@ -92,12 +92,14 @@ service.interceptors.response.use(
// 解密处理 todo 用config.headers['X-Encrypted'] === 'true'判断是否需要解密不一定准确
if (response.config.headers['X-Encrypted'] === 'true') {
try {
res = decryptWithPrivateKey(res, privateKeyHex, 1)
res = decryptWithPrivateKey(res)
} catch (e) {
logger.error('解密响应失败', e)
}
}
res = JSON.parse(res)
if (typeof res === 'string') {
res = JSON.parse(res)
}
endLoading()
if (response.config.back) {
return res