用户登录问题修改

This commit is contained in:
jiang 2024-11-06 10:05:50 +08:00
parent 33cab82ec6
commit 22d2c5dba1
4 changed files with 42 additions and 21 deletions

View File

@ -2,6 +2,7 @@ import { getConfig } from '@/api/config';
export function get() {
getConfig()
.then(response => {
console.log(response)
localStorage.setItem('systemConfig', JSON.stringify(response.data));
})
.catch(error => {

View File

@ -1,7 +1,8 @@
// SM 配置
const SM_CONFIG = {
SALT: '2cc0c5f9f1749f1632efa9f63e902323', // SM3 盐值16 字节)
SM4_KEY: 'your-sm4-key', // SM4 对称加密密钥
SM4_KEY:"78d1295afa99449b99d6f83820e6965c", // SM4 对称加密密钥
SM4_SALT:generateUUID(),
SM2_PUBLIC_KEY: 'your-public-key', // SM2 公钥
SM2_PRIVATE_KEY: 'your-private-key' // SM2 私钥
}
@ -10,6 +11,19 @@ const AES_CONFIG = {
AES_KEY: 'zhgd@bonus@zhgd@bonus@1234567890', // AES key值
AES_IV: '1234567812345678' // AES 偏移量
}
export function generateUUID() {
// 使用当前时间戳和随机数生成一个 UUID
return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0; // 生成随机数
const v = c === 'x' ? r : (r & 0x3 | 0x8); // 根据 UUID 规范生成相应的值
return v.toString(16); // 转换为十六进制
});
}
// 使用示例
const uuid = generateUUID();
console.log(uuid);
module.exports = {
SM_CONFIG,
AES_CONFIG,

View File

@ -7,9 +7,9 @@ import { tansParams, blobValidate } from '@/utils/bonus'
import cache from '@/plugins/cache'
import { saveAs } from 'file-saver'
import { encryptCBC, decryptCBC } from '@/utils/aescbc'
import { hashWithSM3AndSalt } from '@/utils/sm'
const systemConfig = JSON.parse(localStorage.getItem('systemConfig')) || {
requestConfig: { encryptRequest: false, checkIntegrity: false, encryptResponse: false }
import { decryptWithSM4, encryptWithSM4, hashWithSM3AndSalt } from '@/utils/sm'
const systemConfig = {
requestConfig: { encryptRequest: true, checkIntegrity: true, encryptResponse: true }
};
let downloadLoadingInstance
@ -64,14 +64,9 @@ service.interceptors.request.use(config => {
let data = typeof config.data === 'object' ? JSON.stringify(config.data) : config.data
let contentType = config.headers['Content-Type']
if (contentType.includes('application/json') && typeof data !== 'undefined') {
// 数据完整性校验
if (systemConfig.requestConfig.checkIntegrity && checkIntegrity) {
config.headers['Params-Hash'] = hashWithSM3AndSalt(data)
config.data = data
}
// 加密数据
if (systemConfig.requestConfig.encryptRequest && encryptRequest) {
config.data = encryptCBC(data)
config.data = encryptWithSM4(data+"|"+hashWithSM3AndSalt(data))
}
}
// 检查请求数据大小
@ -101,7 +96,7 @@ service.interceptors.request.use(config => {
// 响应拦截器
service.interceptors.response.use(res => {
if (res.headers.encryptresponse && !res.data.hasOwnProperty('code')) {
res.data = JSON.parse(decryptCBC(res.data))
res.data = JSON.parse(decryptWithSM4(res.data))
}
// 未设置状态码则默认成功状态
const code = res.data.code || 200

View File

@ -2,7 +2,8 @@
import { sm2, sm3, sm4 } from 'sm-crypto'
// 配置项例如盐值、SM2 公私钥、SM4 密钥
import { SM_CONFIG } from './configure'
import SM4 from 'sm-crypto/src/sm4'
import { hexToArray } from 'sm-crypto/src/sm2/utils'
// SM3 哈希
export function hashSM3(text) {
@ -29,15 +30,25 @@ export function decryptWithSM2(encryptedText) {
// SM2 私钥解密
return sm2.doDecrypt(encryptedText, SM_CONFIG.SM2_PRIVATE_KEY)
}
// SM4 加密
export function encryptWithSM4(text) {
// SM4 对称加密ECB 模式
return sm4.encrypt(text, SM_CONFIG.SM4_KEY)
/**
* 加密函数
* @param {string} plainText
* @returns {string} 加密后的密文Hex 编码格式
*/
export function encryptWithSM4(plainText) {
const salt =SM_CONFIG.SM4_SALT
return sm4.encrypt(plainText, SM_CONFIG.SM4_KEY,{ mode: 'cbc', padding: 'pkcs#5',iv:salt})+salt;
}
// SM4 解密
export function decryptWithSM4(encryptedText) {
// SM4 对称解密ECB 模式
return sm4.decrypt(encryptedText, SM_CONFIG.SM4_KEY)
/**
* 解密函数
* @param {string} cipherText
* @returns {string} 解密后的明文
*/
export function decryptWithSM4(cipherText){
const length = cipherText.length;
const salt = length > 32 ? cipherText.substring(length - 32) : cipherText;
const originalHex = length > 32 ? cipherText.substring(0, length - 32) : '';
return SM4.decrypt(originalHex, SM_CONFIG.SM4_KEY,{ mode: 'cbc', padding: 'pkcs#5' ,iv:salt});
}