Browse Source

feat:增加接口解密;请求头(hulk-auth)加密;

dxj
李朋徽 1 year ago
parent
commit
35dba0861e
  1. 10
      src/store/moules/userStore/index.ts
  2. 18
      src/utils/axios/index.ts
  3. 4
      src/utils/index.ts

10
src/store/moules/userStore/index.ts

@ -43,6 +43,8 @@ export const useUserStore = defineStore('useUserStore', {
async login(params: TokenParams) {
return new Promise<any>((resolve, reject) => {
token(params).then((res) => {
console.log(res)
this.setToken(crypto.encryptAES(res.access_token, crypto.localKey))
this.setUserInfo(crypto.encryptAES(JSON.stringify(res), crypto.localKey))
this.getChatInfoFun()
@ -63,10 +65,10 @@ export const useUserStore = defineStore('useUserStore', {
* @description: logout
*/
async logout(goLogin = false) {
// this.$reset()
// localStorage.clear()
// // 清空数据
// goLogin && router.push(PageEnum.BASE_LOGIN)
this.$reset()
localStorage.clear()
// 清空数据
goLogin && router.push(PageEnum.BASE_LOGIN)
},
},
persist: [

18
src/utils/axios/index.ts

@ -13,13 +13,16 @@ import { AxiosRetry } from './axiosRetry'
import { useMessage } from '@/hooks/useMessage'
import { ContentTypeEnum, HttpErrorMsgEnum, HttpSuccessEnum, RequestEnum, ResultEnum } from '@/enums/httpEnum'
import { isEmpty, isNull, isString, isUndefined } from '@/utils/is'
import { deepMerge, setObjToUrlParams } from '@/utils'
import { deepMerge, setObjToUrlParams, strHasArr } from '@/utils'
import crypto from '@/utils/crypto'
import { useUserStore } from '@/store/moules/userStore/index'
const { createMessage, createErrorModal, createSuccessModal } = useMessage()
// 请求白名单,无须token的接口
const whiteList: string[] = ['/login', '/refresh-token']
// 不需要解密接口白名单
const notDecryptWhiteList = ['/hulk-auth/oauth/token']
/**
* @description: 便
@ -40,12 +43,17 @@ const transform: AxiosTransform = {
// 不进行任何处理,直接返回
// 用于页面代码可能需要直接获取code,data,message这些信息时开启
if (!isTransformResponse)
if (!isTransformResponse) {
if (strHasArr(res.config.url!, notDecryptWhiteList)) {
return res.data
}
else {
return JSON.parse(crypto.decryptAES(res.data as unknown as string, crypto.aesKey))
}
}
// 错误的时候返回
const { data } = res
const data = JSON.parse(crypto.decryptAES(res.data as unknown as string, crypto.aesKey))
if (!data) {
// return '[HTTP] Request has no return value';
@ -189,7 +197,7 @@ const transform: AxiosTransform = {
if (token && !isToken) {
// jwt token
(config as Recordable).headers[import.meta.env.VITE_GLOB_APP_TOKEN_KEY] = options.tokenScheme
? `${options.tokenScheme} ${token}`
? `${options.tokenScheme} ${crypto.encryptAES(token, crypto.cryptoKey)}`
: token
}

4
src/utils/index.ts

@ -79,3 +79,7 @@ export function openWindow(
window.open(url, target, feature.join(','))
}
export function strHasArr(str: string, arr: string[]) {
return arr.some(item => str.includes(item))
}

Loading…
Cancel
Save