From 3e89c69a95b78d37a6e4204a4f2f5685b862aaf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=87=E6=B0=B8=E8=BE=BE?= Date: Mon, 12 Jun 2023 09:39:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AF=E5=BC=95=E5=85=A5jsrsasign?= =?UTF-8?q?=E8=BF=9B=E8=A1=8CRSA=E5=8A=A0=E5=AF=86=EF=BC=8C=E7=99=BB?= =?UTF-8?q?=E5=BD=95Action=E5=A2=9E=E5=8A=A0RSA=E5=AF=86=E7=A0=81=E5=8A=A0?= =?UTF-8?q?=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/api/system/login.js | 7 +++++++ src/store/modules/user.js | 43 +++++++++++++++++++++------------------ src/utils/jsencrypt.js | 7 ++++++- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 79679a0..c71beee 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "js-cookie": "3.0.1", "js-md5": "^0.7.3", "jsencrypt": "3.2.1", + "jsrsasign": "^10.8.6", "md-editor-v3": "^1.11.11", "nprogress": "0.2.0", "pinia": "^2.0.33", diff --git a/src/api/system/login.js b/src/api/system/login.js index 303333a..83bc355 100644 --- a/src/api/system/login.js +++ b/src/api/system/login.js @@ -67,3 +67,10 @@ export function oauthCallback(data, params) { params: params }) } + +export function getRsaKey() { + return request({ + url: '/getRsaKey', + method: 'get' + }) +} diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 8b04ad1..bc7f1f1 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,7 +1,7 @@ -import { login, logout, getInfo, oauthCallback } from '@/api/system/login' +import { login, logout, getInfo, oauthCallback, getRsaKey } from '@/api/system/login' import { getToken, setToken, removeToken } from '@/utils/auth' import defAva from '@/assets/images/profile.jpg' -import md5 from 'js-md5' +import { encryptByPublicKey } from '@/utils/jsencrypt' const useUserStore = defineStore('user', { state: () => ({ userInfo: '', @@ -20,25 +20,28 @@ const useUserStore = defineStore('user', { }, // 登录 login(userInfo) { - const username = userInfo.username.trim() - const password = md5(userInfo.password) - const code = userInfo.code - const uuid = userInfo.uuid return new Promise((resolve, reject) => { - login(username, password, code, uuid) - .then((res) => { - if (res.code == 200) { - setToken(res.data) - this.token = res.data - resolve() //then处理 - } else { - console.log('login error ', res) - reject(res) //catch处理 - } - }) - .catch((error) => { - reject(error) - }) + getRsaKey().then((response) => { + const publicKey = response.data.publicKey + const username = userInfo.username.trim() + const password = encryptByPublicKey(userInfo.password, publicKey) + const code = userInfo.code + const uuid = userInfo.uuid + login(username, password, code, uuid) + .then((res) => { + if (res.code == 200) { + setToken(res.data) + this.token = res.data + resolve() //then处理 + } else { + console.log('login error ', res) + reject(res) //catch处理 + } + }) + .catch((error) => { + reject(error) + }) + }) }) }, /** diff --git a/src/utils/jsencrypt.js b/src/utils/jsencrypt.js index a758068..0181419 100644 --- a/src/utils/jsencrypt.js +++ b/src/utils/jsencrypt.js @@ -1,5 +1,5 @@ import JSEncrypt from 'jsencrypt/bin/jsencrypt.min' - +import jsrsasign from 'jsrsasign' // 密钥对生成 http://web.chacuo.net/netrsakeypair const publicKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALj0zjON+EVdBsnMcR4Uj+jOYgp5ZipftQZ1utW8KvVioz+RSaotF1JHt59q9SC/mZcWWpbpcEqQ3WyyyCC33msCAwEAAQ==' @@ -20,3 +20,8 @@ export function decrypt(txt) { encryptor.setPrivateKey(privateKey) // 设置私钥 return encryptor.decrypt(txt) // 对数据进行解密 } + +export const encryptByPublicKey = (txt, publicKey) => { + const pubKey = jsrsasign.KEYUTIL.getKey(publicKey) + return jsrsasign.KJUR.crypto.Cipher.encrypt(txt, pubKey) +}