up. 双向数据加密
This commit is contained in:
parent
12b16e56d7
commit
8514062951
@ -34,5 +34,8 @@ export const MAP_KEY = '006d995d433058322319fa797f2876f5';
|
|||||||
/** EleAdminPlus授权码 */
|
/** EleAdminPlus授权码 */
|
||||||
export const LICENSE_CODE = import.meta.env.VITE_LICENSE;
|
export const LICENSE_CODE = import.meta.env.VITE_LICENSE;
|
||||||
|
|
||||||
|
/** 数据加密key */
|
||||||
export const DATA_SECRET_KEY = "mySecretKey123!"
|
export const DATA_SECRET_KEY = "mySecretKey123!"
|
||||||
|
|
||||||
|
/** 请求加密开关 */
|
||||||
|
export const REQUEST_ENCRYPTION = import.meta.env.REQUEST_ENCRYPTION;
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import type { AxiosResponse, InternalAxiosRequestConfig, Method } from 'axios';
|
import type { AxiosResponse, InternalAxiosRequestConfig, Method } from 'axios';
|
||||||
import { unref } from 'vue';
|
import { unref } from 'vue';
|
||||||
import { API_BASE_URL, LAYOUT_PATH, DATA_SECRET_KEY } from '@/config/setting';
|
import { API_BASE_URL, LAYOUT_PATH, DATA_SECRET_KEY, REQUEST_ENCRYPTION } from '@/config/setting';
|
||||||
import type { ApiResult } from '@/api';
|
import type { ApiResult } from '@/api';
|
||||||
import router from '@/router';
|
import router from '@/router';
|
||||||
import { isWhiteList } from '@/router/routes';
|
import { isWhiteList } from '@/router/routes';
|
||||||
@ -44,11 +44,7 @@ export function decryptData(ciphertext: string): any {
|
|||||||
*/
|
*/
|
||||||
export function requestInterceptor(config: SecureRequestConfig) {
|
export function requestInterceptor(config: SecureRequestConfig) {
|
||||||
const method = config.method?.toLowerCase() as Method;
|
const method = config.method?.toLowerCase() as Method;
|
||||||
// 是否加密
|
|
||||||
const shouldEncrypt = (
|
|
||||||
!config.skipEncryption &&
|
|
||||||
config.data &&
|
|
||||||
['post', 'put', 'delete', 'patch'].includes(method));
|
|
||||||
|
|
||||||
// 添加客户端信息到header
|
// 添加客户端信息到header
|
||||||
const client = useClientStore();
|
const client = useClientStore();
|
||||||
@ -64,10 +60,16 @@ export function requestInterceptor(config: SecureRequestConfig) {
|
|||||||
config.headers['Authorization'] = token;
|
config.headers['Authorization'] = token;
|
||||||
}
|
}
|
||||||
// get请求处理数组和对象类型参数
|
// get请求处理数组和对象类型参数
|
||||||
if (config.method === 'get' && config.params) {
|
if (method === 'get' && config.params) {
|
||||||
config.url = toURLSearch(config.params, config.url);
|
config.url = toURLSearch(config.params, config.url);
|
||||||
config.params = {};
|
config.params = {};
|
||||||
}
|
}
|
||||||
|
if (REQUEST_ENCRYPTION) {
|
||||||
|
// 是否加密
|
||||||
|
const shouldEncrypt = (
|
||||||
|
!config.skipEncryption &&
|
||||||
|
config.data &&
|
||||||
|
['post', 'put', 'delete', 'patch'].includes(method));
|
||||||
if (shouldEncrypt && !config.headers?.['X-Encrypted']) {
|
if (shouldEncrypt && !config.headers?.['X-Encrypted']) {
|
||||||
config.data = {
|
config.data = {
|
||||||
encryptedData: encryptData(config.data)
|
encryptedData: encryptData(config.data)
|
||||||
@ -75,6 +77,7 @@ export function requestInterceptor(config: SecureRequestConfig) {
|
|||||||
config.headers['X-Encrypted'] = true;
|
config.headers['X-Encrypted'] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 响应拦截处理
|
* 响应拦截处理
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user