js cookies all in one

cookies


// http://10.1.5.202/auto-deploy-platform/publish/index.html // 非当前 URL 的 domain / path 下, 创建的 cookie 无法写入到当前的 域名/路径下
// domain 不同,cookie 不能写入
document.cookie = "token=1234567; Expires=Wed, 21 Oct 2020 07:28:00 GMT; path=/; domain=cdn.xgqfrms.xyz;";
// path 不同(Ajax path),cookie 不能写入
document.cookie = "token=1234567; Expires=Wed, 21 Oct 2020 07:28:00 GMT; path=/deployprod/;" // 当前 URL 的 domain & path 下, 创建的 cookie 可以写入到当前的 域名/路径下
// 默认 domain === 当前 URL 的 domain
document.cookie = "token=1234567; Expires=Wed, 21 Oct 2020 07:28:00 GMT; path=/;";
// 当前 URL 的 domain & path 下,cookie 可以写入
document.cookie = "token_path1=1234567; Expires=Wed, 21 Oct 2020 07:28:00 GMT; path=/auto-deploy-platform/;"
// 当前 URL 的 domain & path 下,cookie 可以写入
document.cookie = "token_path2=1234567; Expires=Wed, 21 Oct 2020 07:28:00 GMT; path=/auto-deploy-platform/publish;"



function cookies() {
var D = new Date();
D.setDate(D.getDate() + 30);
document.cookie = "author=xgqfrms; website=www.xgqfrms.xyz; domain=xgqfrms.xyz; secure ;expire=" + D;
/*
// read cookie
var rc = document.cookie.substring(5);
if (rc != "www.xgqfrms.xyz") {
alert("cookie is empty!"+rc);
var D = new Date();
D.setDate(D.getDate()+30);
//write cookie
document.cookie="name=www.xgqfrms.xyz;domain=xgqfrms.xyz;HttpOnly;expire="+D;
}else{
alert("cookie is:"+rc);
}
*/
};
cookies();

view-source:https://www.xgqfrms.xyz/


cookies & url bug / domain bug???

URL 与 请求API 路经不一致,如何写入 cookie 并且请求 API 的时候带上 cookies ?

  1. 正常都是 URL 与 请求API 路经一一对应的
// URL (domain)
https://cdn.xgqfrms.xyz/json // API
https://cdn.xgqfrms.xyz/json/api/data.json

  1. jwt 添加一个 dev / prod 字段

// access_token=eyJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoiQWRtaW4iLCJleHBpcmVkIjoxNTM5ODQxMDQ2NjAwLCJ1c2VyX25hbWUiOiJhZG1pbiIsImVtYWlsIjoiaG9uZ2trQGdpbGRhdGEuY29tIn0.o453AHiAlgE90YHiCAOcPDn5__EfhB7ujaGtjx_xfqg"



{
"data": {
"cookies": "jwt_access_token",
"prod": false,
"dev": true
}
}


if(document.cookie !== "" && document.cookie.includes(`access_token`)) {
let token = JSON.parse(decodeURIComponent(atob(document.cookie.replace(`access_token=`, ``).split(`.`)[1])));
// console.log(`token =`, JSON.stringify(token, null, 4));
let {
// role,
user_name,
email
} = token;
sessionStorage.setItem(`email`, email);
layui.use(["element", "layer", "form"], function() {
let element = layui.element,
form = layui.form,
layer = layui.layer;
// layui
showRoleName(user_name);
logoutHandler();
modifyEmail();
modifyPassword();
});
} else {
swal({
title: "未登录用户,无权访问!",
text: `
请先登录后,再访问!\n
1 秒后自动关闭.
`,
icon: "warning",
className: "warning-alert-style",
timer: 2000,
button: {
text: "关闭",
value: true,
visible: true,
closeModal: true
}
});
setTimeout(() => {
autoRedirectToLogin();
}, 1000);
}

最新文章

  1. linux中redis的php扩展安装
  2. ffmpeg-20160908[09,10,13,15,19,21,22,24]-bin.7z
  3. python数据结构与算法——队列
  4. Java事务处理全解析(七)—— 像Spring一样使用Transactional注解(Annotation)
  5. Android——AutoCompleteTextView、Spinner和消息提示
  6. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
  7. 响应式布局中重要的meta标签设置.适用于手机浏览器兼容性设置
  8. 公交线路免费api接口代码
  9. J2EE struts2MVC应用在线书签1
  10. 分布式文件系统FastDFS如何做到高可用
  11. hsdf -- 6.21 -- day6
  12. 刚学习java时的笔记, 有点渣, 毕竟都是从低往高走
  13. js中 函数声明/函数表达式/匿名函数/箭头函数/立即执行函数
  14. windows下navicate for mysql 零填充不显示
  15. mod_fcgid: HTTP request length 136136 (so far) exceeds MaxRequestLen (131072)
  16. webGl中实现clipplane
  17. Unity3D学习笔记(二):个体层次、绝对和局部坐标、V3平移旋转
  18. win10安装.net framework3.5
  19. ORB算法介绍(转)
  20. 【bzoj1004】[HNOI2008]Cards Burnside引理+背包dp

热门文章

  1. bzoj 1034: [ZJOI2008]泡泡堂BNB【贪心】
  2. Django day 37 网站视频的播放,购物车接口,优惠券表分析
  3. 微服务下,使用ELK做日志收集及分析
  4. CSS布局大全
  5. mysql的大数据量的查询
  6. thinkphp 5 常用的助手函数
  7. webservice 权限控制
  8. RecycleView的简单应用
  9. 【IIS7.5】Asp文件上传限制,加载页面大小限制
  10. Java 8 (2) 使用Lambda表达式