(improvement)(login) encrypt password (#1081) (#1116)

This commit is contained in:
zhaodongsheng
2024-06-09 08:16:45 +08:00
committed by GitHub
parent dcb7f21241
commit 5bc88b78a9
15 changed files with 289 additions and 95 deletions

View File

@@ -13,6 +13,7 @@ import { postUserLogin, userRegister } from './services';
import { AUTH_TOKEN_KEY } from '@/common/constants';
import { queryCurrentUser } from '@/services/user';
import { history, useModel } from 'umi';
import {encryptPassword} from "@/utils/utils";
const { Item } = Form;
const LoginPage: React.FC = () => {
@@ -43,21 +44,24 @@ const LoginPage: React.FC = () => {
message.success(msg);
};
// 处理登录按钮响应
const handleLogin = async () => {
const { validateFields } = form;
const content = await validateFields();
await loginDone(content);
await loginDone({...content, password: encryptPassword(content.password)});
};
// 处理注册弹窗确定按钮
const handleRegister = async (values: RegisterFormDetail) => {
const { code } = await userRegister({ ...values });
const enCodeValues = { ...values, password: encryptPassword(values.password) };
const { code } = await userRegister(enCodeValues);
if (code === 200) {
message.success('注册成功');
setCreateModalVisible(false);
// 注册完自动帮用户登录
await loginDone(values);
await loginDone(enCodeValues);
}
};
@@ -103,7 +107,7 @@ const LoginPage: React.FC = () => {
<Input
size="large"
type="password"
placeholder="密码: admin"
placeholder="密码: admin123"
onPressEnter={handleLogin}
prefix={<LockOutlined />}
/>

View File

@@ -3,6 +3,7 @@ import { message } from 'antd';
import numeral from 'numeral';
import copy from 'copy-to-clipboard';
import { isString } from 'lodash';
import CryptoJS from 'crypto-js';
/* eslint no-useless-escape:0 */
const reg =
@@ -470,3 +471,15 @@ export const objToArray = (_obj: ObjToArrayParams, keyType: string = 'string') =
};
});
};
export function encryptPassword(password: string, username: string) {
if (!password) {
return password;
}
// TODO This key should be stored in a secure place
const key = CryptoJS.enc.Utf8.parse('supersonic@2024');
const srcs = CryptoJS.enc.Utf8.parse(password);
const encrypted = CryptoJS.AES.encrypt(srcs, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7});
return encrypted.toString();
};