提交 61f707bc authored 作者: 钟是志's avatar 钟是志

安全测评 token 放在url 上

上级 ed59e77e
// use localStorage to store the authority info, which might be sent from server in actual project. import { setCookie } from '@/H5Public/utils/cookie';
export function getAuthority() { import { getUrlParams } from '@/H5Public/utils/handleString';
return localStorage.getItem('antd-pro-authority') || 'admin';
}
export function setAuthority(authority) {
return localStorage.setItem('antd-pro-authority', authority);
}
export function getLoginToUrl() {
return localStorage.getItem('antd-pro-login-url');
}
export function setLoginToUrl(url) { export function setLoginToUrl(url) {
return localStorage.setItem('antd-pro-login-url', url); return localStorage.setItem('antd-pro-login-url', url);
} }
export function clearToken() { export function clearToken() {
localStorage.removeItem('antd-username'); localStorage.removeItem('antd-username');
return localStorage.removeItem('antd-pro-token'); return localStorage.removeItem('antd-pro-token');
} }
export function setUsername(username) {
if(typeof username === 'object'){
username = JSON.stringify(username);
}
localStorage.setItem('antd-username', username);
}
export function getUsername() {
return localStorage.getItem('antd-username');
}
export function setToken(token) { export function setToken(token) {
setCookie('token', token);
return localStorage.setItem('antd-pro-token', token); return localStorage.setItem('antd-pro-token', token);
} }
export function getToken() { export function getToken() {
let t = localStorage.getItem('antd-pro-token');
let t2 = getUrlParams().token;
if(t2){
setToken(t2)
}
if(!t || t.length < 10){
return t2 || '';
}
return localStorage.getItem('antd-pro-token'); return localStorage.getItem('antd-pro-token');
} }
......
/**
* 设置cookie
* @param name cookie的名称
* @param value cookie的值
* @param day cookie的过期时间
*/
export const setCookie = function(name, value, day) {
if (day !== 0) {
//当设置的时间等于0时,不设置expires属性,cookie在浏览器关闭后删除
const expires = day * 24 * 60 * 60 * 1000;
const date = new Date(+new Date() + expires);
document.cookie = name + '=' + escape(value) + ';expires=' + date.toUTCString() + ';path=/';
} else {
document.cookie = name + '=' + escape(value) + ';path=/';
}
};
/**
* 获取对应名称的cookie
* @param name cookie的名称
* @returns {null} 不存在时,返回null
*/
export const getCookie = function(name) {
let arr;
const reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
arr = document.cookie.match(reg);
if (!!arr) return unescape(arr[2]);
else return null;
};
/**
* 删除cookie
* @param name cookie的名称
*/
export const delCookie = function(name) {
setCookie(name, ' ', -1);
};
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论