加密处理

This commit is contained in:
bb_pan 2025-04-24 14:02:12 +08:00
parent 298a1b771c
commit 077c3518b4
4 changed files with 37 additions and 5 deletions

View File

@ -16,6 +16,7 @@ import ThemePicker from '@/components/ThemePicker'
import Notice from '@/components/Notice'
import { getHomeNoticeApi } from '@/api/system/notice.js'
import { getToken } from '@/utils/auth'
import { getConfigApi } from '@/utils/config'
export default {
name: 'App',
@ -42,6 +43,10 @@ export default {
noticeList: [],
}
},
created() {
//
getConfigApi()
},
updated() {
//
if (

View File

@ -121,3 +121,10 @@ export function checkPasswordStatusApi(data) {
})
}
// 获取系统配置
export const getConfig = () => {
return request({
url: '/auth/getConfig',
method: 'get'
})
}

View File

@ -0,0 +1,11 @@
import { getConfig } from '@/api/login'
export function getConfigApi() {
getConfig()
.then((response) => {
console.log(response)
localStorage.setItem('systemConfig', JSON.stringify(response.data))
})
.catch((error) => {
console.error('Failed to fetch config:', error)
})
}

View File

@ -8,6 +8,10 @@ import cache from '@/plugins/cache'
import { saveAs } from 'file-saver'
import { decryptWithSM4, encryptWithSM4, hashWithSM3AndSalt } from '@/utils/sm'
const systemConfig = JSON.parse(localStorage.getItem('systemConfig')) || {
requestConfig: { encryptRequest: false, checkIntegrity: false, encryptResponse: false }
};
let downloadLoadingInstance;
// 是否显示重新登录
export let isRelogin = { show: false };
@ -27,11 +31,11 @@ service.interceptors.request.use(config => {
const checkIntegrity = false
const encryptResponse = false
//入参加密
config.headers['encryptRequest'] = encryptRequest ? 'true' : 'false'
config.headers['encryptRequest'] = systemConfig.requestConfig.encryptRequest && encryptRequest ? 'true' : 'false'
// 数据完整性校验
config.headers['checkIntegrity'] = checkIntegrity ? 'true' : 'false'
config.headers['checkIntegrity'] = systemConfig.requestConfig.checkIntegrity && checkIntegrity ? 'true' : 'false'
//回参是否加密
config.headers['encryptResponse'] = encryptResponse ? 'true' : 'false'
config.headers['encryptResponse'] = systemConfig.requestConfig.encryptResponse && encryptResponse ? 'true' : 'false'
// console.log('🚀 ~ config:', config)
// 是否需要设置 token
@ -58,8 +62,8 @@ service.interceptors.request.use(config => {
// console.log('🚀 ~ contentType:', contentType, requestObj.data)
if (contentType.includes('application/json') && typeof requestObj.data !== 'undefined') {
// 加密数据
if (encryptRequest) {
console.log(hashWithSM3AndSalt(requestObj.data));
if (systemConfig.requestConfig.encryptRequest && encryptRequest) {
// console.log(hashWithSM3AndSalt(requestObj.data));
config.data = encryptWithSM4(requestObj.data+"|"+hashWithSM3AndSalt(requestObj.data))
}
}
@ -108,6 +112,11 @@ service.interceptors.response.use(res => {
}
if (code === 401) {
if (!isRelogin.show) {
// 判断当前是否已经在登录页面, 在的话阻止继续执行
if (window.location.href.indexOf('/login') !== -1) {
isRelogin.show = false
return
}
isRelogin.show = true;
MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', { confirmButtonText: '重新登录', cancelButtonText: '取消', type: 'warning' }).then(() => {
isRelogin.show = false;