实现数据加密传输,成功调试登录接口
This commit is contained in:
+27
-11
@@ -3,6 +3,9 @@ import { Config } from "@/config.uts"
|
||||
import { HttpMethod } from "@/enum/HttpMethod"
|
||||
import { Auth } from "@/utils/Auth.uts"
|
||||
import { XiaoZhi } from "@/utils/XiaoZhi"
|
||||
import { Crypto } from "@/utils/crypto.uts"
|
||||
import { base64Encode, createRsaPair, rsaEncryptText } from "@/uni_modules/laoqianjunzi-crypto"
|
||||
|
||||
|
||||
/**
|
||||
* HTTP请求客户端
|
||||
@@ -19,6 +22,11 @@ export class HttpClient {
|
||||
|
||||
private static instance : HttpClient | null = null
|
||||
|
||||
private static encryptHeader = 'encrypt-key';
|
||||
private static publicKey = '-----BEGIN PUBLIC KEY-----\n' +
|
||||
Config.VITE_APP_RSA_PUBLIC_KEY +
|
||||
'\n-----END PUBLIC KEY-----';
|
||||
|
||||
/**
|
||||
* 获取单例实例
|
||||
*/
|
||||
@@ -38,7 +46,7 @@ export class HttpClient {
|
||||
* @returns 标准响应对象
|
||||
*/
|
||||
async request<T>(config : RequestConfig) : Promise<ApiResponse<T>> {
|
||||
console.log("请求拦截")
|
||||
console.log("请求拦截,请求地址:" + config.url)
|
||||
|
||||
let url : string = Config.BASE_URL + config.url
|
||||
let data = config.data ?? null
|
||||
@@ -96,14 +104,28 @@ export class HttpClient {
|
||||
*/
|
||||
if (Config.VITE_APP_ENCRYPT) {
|
||||
if (isEncrypt && (config.method === HttpMethod.POST || config.method === HttpMethod.PUT)) {
|
||||
console.log("加密")
|
||||
// AES密钥
|
||||
const aesKey = Crypto.generateAesKey();
|
||||
// Base64(AES_KEY)
|
||||
const aesKeyBase64 = base64Encode(aesKey)
|
||||
|
||||
// RSA加密后的Header值
|
||||
|
||||
|
||||
header[HttpClient.encryptHeader] = rsaEncryptText(HttpClient.publicKey, aesKeyBase64, 'base64')
|
||||
console.log(header[HttpClient.encryptHeader]);
|
||||
const requestData = config.data
|
||||
|
||||
if (requestData != null) {
|
||||
data = Crypto.encryptWithAes(requestData, aesKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (config.loading == true) {
|
||||
LoadingManager.show()
|
||||
}
|
||||
|
||||
console.log("请求拦截,请求地址:" + url)
|
||||
try {
|
||||
return await new Promise<ApiResponse<T>>((resolve, reject) => {
|
||||
uni.request({
|
||||
@@ -118,26 +140,20 @@ export class HttpClient {
|
||||
enableChunked,
|
||||
success: (res) => {
|
||||
const responseData = res["data"] as UTSJSONObject
|
||||
|
||||
console.log(
|
||||
"响应拦截-响应状态码:" +
|
||||
JSON.stringify(responseData["code"])
|
||||
)
|
||||
|
||||
const apiResponse = new ApiResponse<T>()
|
||||
|
||||
if (responseData != null) {
|
||||
apiResponse.code = (responseData["code"] as number) ?? 0
|
||||
apiResponse.msg = (responseData["msg"] as string) ?? ""
|
||||
apiResponse.data = responseData["data"] as T
|
||||
}
|
||||
console.log("响应拦截-响应状态码:" + apiResponse.code)
|
||||
|
||||
resolve(apiResponse)
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(err)
|
||||
},
|
||||
complete: () => {}
|
||||
complete: () => { }
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
+21
-35
@@ -1,55 +1,34 @@
|
||||
import { createRsaPair, base64Decode, base64Encode, } from "@/uni_modules/laoqianjunzi-crypto";
|
||||
// 在插件中,h5不会导出CryptoRsaPair,需要自定义,等待后续作者处理
|
||||
// #ifdef H5
|
||||
type CryptoRsaPair = {
|
||||
publicKey : string;
|
||||
privateKey : string;
|
||||
}
|
||||
// #endif
|
||||
// #ifdef APP-ANDROID
|
||||
import { CryptoRsaPair } from "@/uni_modules/laoqianjunzi-crypto";
|
||||
// #endif
|
||||
import { createRsaPair, base64Decode, base64Encode, defaultTextCipherOptions, aesEncryptText, } from "@/uni_modules/laoqianjunzi-crypto";
|
||||
|
||||
export class Crypto {
|
||||
/**
|
||||
* 随机生成32位的字符串
|
||||
* @returns {string}
|
||||
*/
|
||||
static generateRandomString() : string {
|
||||
const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
let result = ""
|
||||
for (let i = 0; i < 32; i++) {
|
||||
result += chars.charAt(
|
||||
Math.floor(
|
||||
Math.random() * chars.length
|
||||
)
|
||||
)
|
||||
}
|
||||
return result;
|
||||
}
|
||||
static generateRandomString(length = 16) : string {
|
||||
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
||||
let result = ''
|
||||
|
||||
private static readonly rsaBitsText = ref('2048')
|
||||
static currentRsaBits() : number {
|
||||
const parsed = parseInt(Crypto.rsaBitsText.value)
|
||||
if (parsed == 1024 || parsed == 2048 || parsed == 3072 || parsed == 4096) {
|
||||
return parsed
|
||||
for (let i = 0; i < length; i++) {
|
||||
result += chars.charAt(Math.floor(Math.random() * chars.length))
|
||||
}
|
||||
return 2048
|
||||
|
||||
return result
|
||||
}
|
||||
/**
|
||||
* 随机生成aes 密钥
|
||||
* @returns {string}
|
||||
*/
|
||||
static generateAesKey() : CryptoRsaPair {
|
||||
return createRsaPair(Crypto.currentRsaBits());
|
||||
static generateAesKey() : string {
|
||||
return Crypto.generateRandomString(16);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密base64
|
||||
* @returns {string}
|
||||
*/
|
||||
static encryptBase64(str : CryptoRsaPair) : string {
|
||||
return base64Encode(str.privateKey + str.publicKey);
|
||||
static encryptBase64(str : string) : string {
|
||||
return base64Encode(str);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,8 +44,15 @@ export class Crypto {
|
||||
* @param aesKey
|
||||
* @returns {string}
|
||||
*/
|
||||
static encryptWithAes() : string {
|
||||
return '';
|
||||
static encryptWithAes(data : any, aesKey : string) : string {
|
||||
// AES配置
|
||||
const options = defaultTextCipherOptions()
|
||||
options.mode = 'ECB'
|
||||
options.padding = 'PKCS7'
|
||||
options.keyLength = 128
|
||||
// 请求体JSON
|
||||
const json = JSON.stringify(data)
|
||||
return aesEncryptText(aesKey, json, options);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+2
-2
@@ -21,7 +21,7 @@ export class JSEncrypt {
|
||||
* @returns Base64密文
|
||||
*/
|
||||
static encrypt(text : string) : string {
|
||||
if (!text) {
|
||||
if (text == "") {
|
||||
return "";
|
||||
}
|
||||
return rsaEncryptText(JSEncrypt.PUBLIC_KEY, text, "base64");
|
||||
@@ -34,7 +34,7 @@ export class JSEncrypt {
|
||||
* @returns 明文
|
||||
*/
|
||||
static decrypt(cipherText : string) : string {
|
||||
if (!cipherText) {
|
||||
if (cipherText=="") {
|
||||
return "";
|
||||
}
|
||||
return rsaDecryptText(JSEncrypt.PRIVATE_KEY, cipherText, "base64");
|
||||
|
||||
Reference in New Issue
Block a user