diff --git a/package.json b/package.json index 2df9be8..3cf3349 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "axios": "0.24.0", "clipboard": "2.0.8", "core-js": "3.25.3", - "crypto-js": "4.1.1", + "crypto-js": "^4.1.1", "echarts": "5.4.0", "element-ui": "2.15.14", "file-saver": "2.0.5", @@ -52,6 +52,7 @@ "nprogress": "0.2.0", "quill": "1.3.7", "screenfull": "5.0.2", + "sm-crypto": "^0.3.13", "sortablejs": "1.10.2", "vue": "2.6.12", "vue-count-to": "1.0.13", diff --git a/src/api/login.js b/src/api/login.js index 98c6787..2bb9407 100644 --- a/src/api/login.js +++ b/src/api/login.js @@ -1,9 +1,6 @@ import request from '@/utils/request' -import { encrypt } from '@/utils/jsencrypt' - // 登录方法 export function login(username, password, code) { - password = encrypt(password); const data = { username, password, @@ -55,3 +52,11 @@ export function isCaptchaEnabled() { method: 'get' }) } + +// 获取key +export function getPublicKey() { + return request({ + url: '/publicKey', + method: 'get', + }) +} diff --git a/src/api/sm4.js b/src/api/sm4.js new file mode 100644 index 0000000..f0f4c50 --- /dev/null +++ b/src/api/sm4.js @@ -0,0 +1,17 @@ +const sm4 =require('sm-crypto').sm4 + +//const key='7f5cd501e5548914edaed6824d3ff79d'//可以为16进制串或字节数组,要求为128比特 +/** + * 加密 + * @param txt + * @returns {*} + */ +export function encrypt(txt,key) { + console.log(txt) + console.log(key) + return sm4.encrypt(txt,key); +} + +export function decrypt(txt,key) { + return sm4.decrypt(txt,key); +} diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 3d5382b..083721e 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,5 +1,6 @@ -import { login, logout, getInfo } from '@/api/login' +import {login, logout, getInfo, getPublicKey} from '@/api/login' import { getToken, setToken, removeToken } from '@/utils/auth' +import {encrypt} from "@/api/sm4"; const user = { state: { @@ -33,23 +34,39 @@ const user = { }, actions: { + getPublicKey() { + return new Promise((resolve, reject) => { + getPublicKey() + .then(res => { + resolve(res) + }) + .catch(error => { + reject(error) + }) + }) + }, // 登录 - Login({ commit }, userInfo) { - const username = userInfo.username.trim() - const password = userInfo.password - const code = userInfo.code + Login({ commit, dispatch }, userInfo) { return new Promise((resolve, reject) => { - login(username, password, code).then(res => { - setToken(res.token) - commit('SET_TOKEN', res.token) - resolve() - }).catch(error => { - reject(error) + dispatch('getPublicKey').then(res => { + let publicKey = res + const username = userInfo.username.trim() + //调用加密方法(传密码和公钥) + const password = encrypt(userInfo.password,publicKey) + const code = userInfo.code + const uuid = userInfo.uuid + login(username, password, code, uuid) + .then(res => { + setToken(res.token) + commit('SET_TOKEN', res.token) + resolve() + }) + .catch(error => { + reject(error) + }) }) }) }, - - // 获取用户信息 GetInfo({ commit, state }) { return new Promise((resolve, reject) => { diff --git a/src/views/register.vue b/src/views/register.vue index 706c89a..678560a 100644 --- a/src/views/register.vue +++ b/src/views/register.vue @@ -77,7 +77,8 @@