优化 auth 方法
新增加密方法
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
import Cookies from 'js-cookie';
|
import Cookies from 'js-cookie';
|
||||||
|
import { encrypt } from '@/utils/jsencrypt';
|
||||||
|
|
||||||
|
// 30 天
|
||||||
|
export const expiresTime = 30;
|
||||||
|
|
||||||
export const TokenKey = 'Admin-Token'
|
export const TokenKey = 'Admin-Token'
|
||||||
|
|
||||||
@@ -13,3 +17,29 @@ export function setToken(token: string) {
|
|||||||
export function removeToken() {
|
export function removeToken() {
|
||||||
return Cookies.remove(TokenKey)
|
return Cookies.remove(TokenKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface AccountInfo {
|
||||||
|
username?: string;
|
||||||
|
password?: string;
|
||||||
|
rememberMe?: 'true' | 'false';
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getAccount() {
|
||||||
|
return {
|
||||||
|
username: Cookies.get("username"),
|
||||||
|
password: Cookies.get("password"),
|
||||||
|
rememberMe: Cookies.get("rememberMe"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setAccount(account: AccountInfo) {
|
||||||
|
account.username && Cookies.set("username", account.username, { expires: expiresTime});
|
||||||
|
account.password && Cookies.set("password", encrypt(account.password) as string, { expires: expiresTime});
|
||||||
|
account.rememberMe != null && Cookies.set("rememberMe", account.rememberMe, { expires: expiresTime});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function removeAccount() {
|
||||||
|
Cookies.remove("username");
|
||||||
|
Cookies.remove("password");
|
||||||
|
Cookies.remove("rememberMe");
|
||||||
|
}
|
||||||
|
|||||||
31
src/utils/jsencrypt.ts
Normal file
31
src/utils/jsencrypt.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import JSEncrypt from "jsencrypt";
|
||||||
|
|
||||||
|
// 密钥对生成 http://web.chacuo.net/netrsakeypair
|
||||||
|
|
||||||
|
const publicKey =
|
||||||
|
"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKoR8mX0rGKLqzcWmOzbfj64K8ZIgOdH\n" +
|
||||||
|
"nzkXSOVOZbFu/TJhZ7rFAN+eaGkl3C4buccQd/EjEsj9ir7ijT7h96MCAwEAAQ==";
|
||||||
|
|
||||||
|
const privateKey =
|
||||||
|
"MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY\n" +
|
||||||
|
"7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN\n" +
|
||||||
|
"PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA\n" +
|
||||||
|
"kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow\n" +
|
||||||
|
"cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv\n" +
|
||||||
|
"DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh\n" +
|
||||||
|
"YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3\n" +
|
||||||
|
"UP8iWi1Qw0Y=";
|
||||||
|
|
||||||
|
// 加密
|
||||||
|
export function encrypt(txt: string) {
|
||||||
|
const encryptor = new JSEncrypt();
|
||||||
|
encryptor.setPublicKey(publicKey); // 设置公钥
|
||||||
|
return encryptor.encrypt(txt); // 对数据进行加密
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解密
|
||||||
|
export function decrypt(txt: string) {
|
||||||
|
const encryptor = new JSEncrypt();
|
||||||
|
encryptor.setPrivateKey(privateKey); // 设置私钥
|
||||||
|
return encryptor.decrypt(txt); // 对数据进行解密
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user