zoukankan      html  css  js  c++  java
  • cookie的知识点

    问题:单点登录发现,存储于cookie中的token值,在主域相同,子域不同(例如aaa.xxx.com和bbb.xxx.com)的两个域中,会互相影响

    问题原因:

    (1)cookie的作用域是domain本身以及domain下的所有子域名
    (2)cookie基础知识点

    JS操作cookie的增删改查

    (1)查询

    /**
     * 获取cookie
     *
     * @export
     * @param {string} name
     * @returns
     */
    export function getCookie(name) {
      const strcookie = document.cookie // 获取cookie字符串
      const arrcookie = strcookie.split('; ') // 分割
      // 遍历匹配
      // tslint:disable-next-line: prefer-for-of
      for (let i = 0; i < arrcookie.length; i++) {
        const arr = arrcookie[i].split('=')
        if (arr[0] === name) {
          return arr[1]
        }
      }
    
      return ''
    }

    (2)增、改方法相同

    /**
     * 设置cookie
     *
     * @export
     * @param {string} name
     * @param {string} value
     * @param {number} day
     * @param {string} [path='/']
     */
    export function setCookie(
      name,
      value,
      day,
      path = '/'
    ) {
      const d = new Date()
      d.setTime(d.getTime() + 24 * 60 * 60 * 1000 * day)
      window.document.cookie =
        name + '=' + value + ';path=' + path + ';expires=' + d.toUTCString()
    }

    (3)删除

    /**
     * 删除cookie 使cookie过期
     *
     * @export
     * @param {string} name
     * @param {string} [path='/']
     */
    export function deleteCookie(name, path = '/') {
      const value = getCookie(name)
      if (value !== null) {
        const d = new Date()
        d.setTime(d.getTime() - 1)
        window.document.cookie =
          name + '=' + value + ';path=' + path + ';expires=' + d.toUTCString()
      }
    }
  • 相关阅读:
    2021.10 好运气
    2021.9 抢购
    2021.8 全周期工程师
    2021.7 创业者
    2021.6 过年
    jenkins学习17
    httprunner 3.x学习18
    httprunner 3.x学习17
    python笔记57-@property源码解读与使用
    httprunner 3.x学习16
  • 原文地址:https://www.cnblogs.com/zkpThink/p/13063938.html
Copyright © 2011-2022 走看看