feat: add SHA-256 encryption for password handling in login process within App component
This commit is contained in:
@@ -49,6 +49,15 @@
|
|||||||
{ id: 'other', label: '其他' }
|
{ id: 'other', label: '其他' }
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// 添加 SHA-256 加密函数
|
||||||
|
async function sha256(message: string): Promise<string> {
|
||||||
|
const msgBuffer = new TextEncoder().encode(message);
|
||||||
|
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
|
||||||
|
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
||||||
|
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
|
||||||
|
return hashHex;
|
||||||
|
}
|
||||||
|
|
||||||
// 初始化函数:检查认证状态
|
// 初始化函数:检查认证状态
|
||||||
async function initializeAuth() {
|
async function initializeAuth() {
|
||||||
const auth = localStorage.getItem('auth');
|
const auth = localStorage.getItem('auth');
|
||||||
@@ -118,13 +127,13 @@
|
|||||||
async function handleLogin(e: Event) {
|
async function handleLogin(e: Event) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
try {
|
try {
|
||||||
// 保存认证信息
|
// 对密码进行加密
|
||||||
const auth = btoa(`${username}:${password}`);
|
const hashedPassword = await sha256(password);
|
||||||
|
|
||||||
// 使用验证接口验证登录
|
// 使用验证接口验证登录
|
||||||
const response = await request.post<ApiResponse<{token: string}>>('/user/login', {
|
const response = await request.post<ApiResponse<{token: string}>>('/user/login', {
|
||||||
username,
|
username,
|
||||||
password
|
password: hashedPassword
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.data.code === 200) {
|
if (response.data.code === 200) {
|
||||||
|
|||||||
Reference in New Issue
Block a user