自动加解密
This commit is contained in:
parent
ca1072cd99
commit
9f52e6a125
|
|
@ -69,6 +69,7 @@
|
|||
"chalk": "4.1.0",
|
||||
"compression-webpack-plugin": "6.1.2",
|
||||
"connect": "3.6.6",
|
||||
"crypto-js": "^4.2.0",
|
||||
"eslint": "7.15.0",
|
||||
"eslint-plugin-vue": "7.2.0",
|
||||
"lint-staged": "10.5.3",
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
import * as CryptoJS from 'crypto-js'
|
||||
const cbc_key = CryptoJS.enc.Utf8.parse('zhgd@bonus@zhgd@bonus@1234567890')
|
||||
const cbc_iv = CryptoJS.enc.Utf8.parse('1234567812345678')
|
||||
/**
|
||||
* 加解密开关
|
||||
* 默认参数需要加密
|
||||
* @type {boolean}
|
||||
*/
|
||||
const jia_mi=true;
|
||||
/**
|
||||
* 默认后台会自动加密
|
||||
* @type {boolean}
|
||||
*/
|
||||
const jie_mi=true;
|
||||
/**
|
||||
* 加密
|
||||
* @param word
|
||||
* @returns {string}
|
||||
*/
|
||||
export const encryptCBC = function(word) {
|
||||
if(!jia_mi){
|
||||
return word;
|
||||
}
|
||||
const srcs = CryptoJS.enc.Utf8.parse(word)
|
||||
const encrypted = CryptoJS.AES.encrypt(srcs, cbc_key, {
|
||||
iv: cbc_iv,
|
||||
mode: CryptoJS.mode.CBC,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
})
|
||||
return encrypted.toString()
|
||||
}
|
||||
/**
|
||||
* 解密
|
||||
* @param word
|
||||
* @returns {*}
|
||||
*/
|
||||
export const decryptCBC = function(word) {
|
||||
if(!jie_mi){
|
||||
return word;
|
||||
}
|
||||
const encrypted = CryptoJS.AES.decrypt(word, cbc_key, {
|
||||
iv: cbc_iv,
|
||||
mode: CryptoJS.mode.CBC,
|
||||
padding: CryptoJS.pad.Pkcs7
|
||||
})
|
||||
return encrypted.toString(CryptoJS.enc.Utf8)
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ import errorCode from '@/utils/errorCode'
|
|||
import { tansParams, blobValidate } from "@/utils/bonus";
|
||||
import cache from '@/plugins/cache'
|
||||
import { saveAs } from 'file-saver'
|
||||
import { encryptCBC, decryptCBC } from '@/utils/aescbc'
|
||||
|
||||
let downloadLoadingInstance;
|
||||
// 是否显示重新登录
|
||||
|
|
@ -29,14 +30,20 @@ service.interceptors.request.use(config => {
|
|||
if (getToken() && !isToken) {
|
||||
config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
|
||||
}
|
||||
console.log(typeof (config.data));
|
||||
// get请求映射params参数
|
||||
if (config.method === 'get' && config.params) {
|
||||
let url = config.url + '?' + tansParams(config.params);
|
||||
url = url.slice(0, -1);
|
||||
let param=tansParams(config.params);
|
||||
if(param){
|
||||
param = param.slice(0, -1);
|
||||
param=encryptCBC(param);
|
||||
}
|
||||
let url = config.url + '?' + param;
|
||||
config.params = {};
|
||||
config.url = url;
|
||||
}
|
||||
if (!isRepeatSubmit && (config.method === 'post' || config.method === 'put')) {
|
||||
|
||||
const requestObj = {
|
||||
url: config.url,
|
||||
data: typeof config.data === 'object' ? JSON.stringify(config.data) : config.data,
|
||||
|
|
@ -65,6 +72,16 @@ service.interceptors.request.use(config => {
|
|||
}
|
||||
}
|
||||
}
|
||||
console.log(config);
|
||||
if( config.headers['Content-Type']=='application/json;charset=utf-8'){
|
||||
if(typeof (config.data)=='object'){
|
||||
config.data = encryptCBC(JSON.stringify(config.data))
|
||||
config.headers['Content-Type']='application/json';
|
||||
}
|
||||
|
||||
}
|
||||
console.log(config);
|
||||
|
||||
return config
|
||||
}, error => {
|
||||
console.log(error)
|
||||
|
|
@ -73,6 +90,13 @@ service.interceptors.request.use(config => {
|
|||
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(res => {
|
||||
//自动解密
|
||||
console.log("返回数据")
|
||||
console.log("res.data.decrypt=="+res.data.decrypt)
|
||||
if(typeof res.data.decrypt!='undefined' && res.data.decrypt){
|
||||
const resultData=decryptCBC(res.data.data);
|
||||
res.data=JSON.parse(resultData);
|
||||
}
|
||||
// 未设置状态码则默认成功状态
|
||||
const code = res.data.code || 200;
|
||||
// 获取错误信息
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ function resolve(dir) {
|
|||
|
||||
const CompressionPlugin = require('compression-webpack-plugin')
|
||||
|
||||
const name = process.env.VUE_APP_TITLE || '博诺思管理系统' // 网页标题
|
||||
const name = process.env.VUE_APP_TITLE || '智慧工地后台管理系统' // 网页标题
|
||||
|
||||
const port = process.env.port || process.env.npm_config_port || 80 // 端口
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue