前端引入jsrsasign进行RSA加密,登录Action增加RSA密码加密

This commit is contained in:
文永达 2023-06-12 09:39:50 +08:00
parent 959e8976e0
commit 3e89c69a95
4 changed files with 37 additions and 21 deletions

View File

@ -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",

View File

@ -67,3 +67,10 @@ export function oauthCallback(data, params) {
params: params
})
}
export function getRsaKey() {
return request({
url: '/getRsaKey',
method: 'get'
})
}

View File

@ -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)
})
})
})
},
/**

View File

@ -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)
}