登录接口加密传输登录信息
This commit is contained in:
@@ -26,6 +26,7 @@
|
|||||||
"md5": "^2.3.0",
|
"md5": "^2.3.0",
|
||||||
"mitt": "^3.0.1",
|
"mitt": "^3.0.1",
|
||||||
"mkdirp": "^3.0.1",
|
"mkdirp": "^3.0.1",
|
||||||
|
"node-forge": "^1.3.1",
|
||||||
"nprogress": "^0.2.0",
|
"nprogress": "^0.2.0",
|
||||||
"pinia": "^2.2.1",
|
"pinia": "^2.2.1",
|
||||||
"pinia-plugin-persistedstate": "^3.2.1",
|
"pinia-plugin-persistedstate": "^3.2.1",
|
||||||
@@ -45,6 +46,7 @@
|
|||||||
"@types/html2canvas": "^1.0.0",
|
"@types/html2canvas": "^1.0.0",
|
||||||
"@types/md5": "^2.3.2",
|
"@types/md5": "^2.3.2",
|
||||||
"@types/node": "^20.14.14",
|
"@types/node": "^20.14.14",
|
||||||
|
"@types/node-forge": "^1.3.11",
|
||||||
"@types/nprogress": "^0.2.0",
|
"@types/nprogress": "^0.2.0",
|
||||||
"@types/qs": "^6.9.8",
|
"@types/qs": "^6.9.8",
|
||||||
"@types/sortablejs": "^1.15.2",
|
"@types/sortablejs": "^1.15.2",
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ export namespace Login {
|
|||||||
export interface ReqLoginForm {
|
export interface ReqLoginForm {
|
||||||
username: string;
|
username: string;
|
||||||
password: string;
|
password: string;
|
||||||
|
checked: boolean;
|
||||||
}
|
}
|
||||||
export interface ResLogin {
|
export interface ResLogin {
|
||||||
accessToken: string;
|
accessToken: string;
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import type { Dict } from '@/api/interface'
|
|||||||
* @name 登录模块
|
* @name 登录模块
|
||||||
*/
|
*/
|
||||||
// 用户登录
|
// 用户登录
|
||||||
export const loginApi = (params: Login.ReqLoginForm) => {
|
export const loginApi = (params: { username: string; password: string }) => {
|
||||||
return http.post<Login.ResLogin>(`${rePrefix}/login`, params, { loading: false })
|
return http.post<Login.ResLogin>(`${rePrefix}/login`, params, { loading: false })
|
||||||
// return http.post<Login.ResLogin>(`/Register1`, params, { loading: false })
|
// return http.post<Login.ResLogin>(`/Register1`, params, { loading: false })
|
||||||
}
|
}
|
||||||
@@ -43,3 +43,9 @@ export const getCurrentScene = () => {
|
|||||||
return http.get('/sysTestConfig/getCurrentScene', {}, { loading: false })
|
return http.get('/sysTestConfig/getCurrentScene', {}, { loading: false })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取RSA公钥
|
||||||
|
*/
|
||||||
|
export const getPublicKey = () => {
|
||||||
|
return http.get('/admin/getPublicKey', {}, { loading: false })
|
||||||
|
}
|
||||||
|
|||||||
@@ -80,7 +80,8 @@ const authStore = useAuthStore();
|
|||||||
const modeStore = useModeStore();
|
const modeStore = useModeStore();
|
||||||
const AppSceneStore = useAppSceneStore();
|
const AppSceneStore = useAppSceneStore();
|
||||||
import { useTheme } from "@/hooks/useTheme";
|
import { useTheme } from "@/hooks/useTheme";
|
||||||
import { useI18n } from "vue-i18n"; // 引入 vue-i18n 钩子
|
import { useI18n } from "vue-i18n";
|
||||||
|
import {getPublicKey} from "@/api/user/login"; // 引入 vue-i18n 钩子
|
||||||
const { changePrimary} = useTheme();
|
const { changePrimary} = useTheme();
|
||||||
|
|
||||||
// 初始化 i18n
|
// 初始化 i18n
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ export const useUserStore = defineStore({
|
|||||||
// Set setUserInfo
|
// Set setUserInfo
|
||||||
setUserInfo(userInfo: UserState["userInfo"]) {
|
setUserInfo(userInfo: UserState["userInfo"]) {
|
||||||
this.userInfo = userInfo;
|
this.userInfo = userInfo;
|
||||||
},
|
}
|
||||||
},
|
},
|
||||||
persist: piniaPersistConfig(USER_STORE_KEY),
|
persist: piniaPersistConfig(USER_STORE_KEY),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -29,6 +29,9 @@
|
|||||||
</template>
|
</template>
|
||||||
</el-input>
|
</el-input>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
|
<el-form-item prop='checked'>
|
||||||
|
<el-checkbox v-model="loginForm.checked">记住我</el-checkbox>
|
||||||
|
</el-form-item>
|
||||||
</el-form>
|
</el-form>
|
||||||
<div class='login-btn'>
|
<div class='login-btn'>
|
||||||
<el-button
|
<el-button
|
||||||
@@ -58,16 +61,17 @@ import { HOME_URL } from '@/config'
|
|||||||
import {getTimeState} from '@/utils'
|
import {getTimeState} from '@/utils'
|
||||||
import {type Dict} from '@/api/interface'
|
import {type Dict} from '@/api/interface'
|
||||||
import {type Login} from '@/api/user/interface/user'
|
import {type Login} from '@/api/user/interface/user'
|
||||||
|
import type {ElForm} from 'element-plus'
|
||||||
import {ElNotification} from 'element-plus'
|
import {ElNotification} from 'element-plus'
|
||||||
import { getDictList, loginApi } from '@/api/user/login'
|
import {getDictList, getPublicKey, loginApi} from '@/api/user/login'
|
||||||
import {useUserStore} from '@/stores/modules/user'
|
import {useUserStore} from '@/stores/modules/user'
|
||||||
import {useTabsStore} from '@/stores/modules/tabs'
|
import {useTabsStore} from '@/stores/modules/tabs'
|
||||||
import {useKeepAliveStore} from '@/stores/modules/keepAlive'
|
import {useKeepAliveStore} from '@/stores/modules/keepAlive'
|
||||||
import {initDynamicRouter} from '@/routers/modules/dynamicRouter'
|
import {initDynamicRouter} from '@/routers/modules/dynamicRouter'
|
||||||
import {CircleClose, UserFilled} from '@element-plus/icons-vue'
|
import {CircleClose, UserFilled} from '@element-plus/icons-vue'
|
||||||
import {useAuthStore} from '@/stores/modules/auth'
|
import {useAuthStore} from '@/stores/modules/auth'
|
||||||
import type { ElForm } from 'element-plus'
|
|
||||||
import {useDictStore} from "@/stores/modules/dict";
|
import {useDictStore} from "@/stores/modules/dict";
|
||||||
|
import forge from 'node-forge'
|
||||||
|
|
||||||
const authStore = useAuthStore()
|
const authStore = useAuthStore()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
@@ -76,6 +80,8 @@ const tabsStore = useTabsStore()
|
|||||||
const keepAliveStore = useKeepAliveStore()
|
const keepAliveStore = useKeepAliveStore()
|
||||||
|
|
||||||
const dictStore = useDictStore()
|
const dictStore = useDictStore()
|
||||||
|
const isAutoLogin = ref(false)
|
||||||
|
let publicKey: any = null;
|
||||||
|
|
||||||
|
|
||||||
type FormInstance = InstanceType<typeof ElForm>;
|
type FormInstance = InstanceType<typeof ElForm>;
|
||||||
@@ -89,6 +95,7 @@ const loading = ref(false)
|
|||||||
const loginForm = reactive<Login.ReqLoginForm>({
|
const loginForm = reactive<Login.ReqLoginForm>({
|
||||||
username: '',
|
username: '',
|
||||||
password: '',
|
password: '',
|
||||||
|
checked: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
// login
|
// login
|
||||||
@@ -100,9 +107,18 @@ const login = (formEl: FormInstance | undefined) => {
|
|||||||
try {
|
try {
|
||||||
// 1.执行登录接口
|
// 1.执行登录接口
|
||||||
const {data} = await loginApi({
|
const {data} = await loginApi({
|
||||||
...loginForm,
|
username: forge.util.encode64(loginForm.username),
|
||||||
password: loginForm.password,
|
password: isAutoLogin.value ? loginForm.password : encryptPassword(loginForm.password)
|
||||||
})
|
})
|
||||||
|
if (loginForm.checked) {
|
||||||
|
localStorage.setItem("loginInfo", JSON.stringify({
|
||||||
|
username: forge.util.encode64(loginForm.username),
|
||||||
|
password: isAutoLogin.value ? loginForm.password : encryptPassword(loginForm.password),
|
||||||
|
exp: Date.now() + 1000 * 60 * 60 * 24 * 30
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
localStorage.removeItem("loginInfo")
|
||||||
|
}
|
||||||
userStore.setAccessToken(data.accessToken)
|
userStore.setAccessToken(data.accessToken)
|
||||||
userStore.setRefreshToken(data.refreshToken)
|
userStore.setRefreshToken(data.refreshToken)
|
||||||
userStore.setUserInfo(data.userInfo)
|
userStore.setUserInfo(data.userInfo)
|
||||||
@@ -148,6 +164,33 @@ onMounted(() => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const encryptPassword = (password: string) => {
|
||||||
|
const encrypted = publicKey.encrypt(password, 'RSAES-PKCS1-V1_5');
|
||||||
|
// 将加密后的数据转换为base64格式以便传输
|
||||||
|
return forge.util.encode64(encrypted);
|
||||||
|
}
|
||||||
|
|
||||||
|
onBeforeMount(async () => {
|
||||||
|
let loginInfoJSON = localStorage.getItem("loginInfo")
|
||||||
|
if (loginInfoJSON) {
|
||||||
|
const loginInfo = JSON.parse(loginInfoJSON)
|
||||||
|
if (loginInfo.exp < Date.now()) {
|
||||||
|
localStorage.removeItem("loginInfo")
|
||||||
|
} else {
|
||||||
|
isAutoLogin.value = true
|
||||||
|
loginForm.username = forge.util.decode64(loginInfo.username)
|
||||||
|
loginForm.password = loginInfo.password
|
||||||
|
loginForm.checked = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isAutoLogin.value) {
|
||||||
|
let {data: publicKeyBase64}:{data:string} = await getPublicKey()
|
||||||
|
//将base64格式的公钥转换为Forge可以使用的格式
|
||||||
|
const publicKeyDer = forge.util.decode64(publicKeyBase64);
|
||||||
|
publicKey = forge.pki.publicKeyFromPem(forge.pki.publicKeyToPem(forge.pki.publicKeyFromAsn1(forge.asn1.fromDer(publicKeyDer))));
|
||||||
|
}
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped lang='scss'>
|
<style scoped lang='scss'>
|
||||||
|
|||||||
Reference in New Issue
Block a user