div.example { background-color: rgba(229, 236, 243, 1); color: rgba(0, 0, 0, 1); padding: 0.5em; margin: 1em 2em 1em 1em }
div.warning { border: 1px solid rgba(255, 0, 0, 1) }

1.cookie中的domain代表的是cookie所在的域,默认情况下就是请求的域名,例如请求http://www.server1.com/files/hello, 那么响应中的set-Cookie默认会使用www.server1.com作为cookie的domain,在浏览器中也是按照domain来组织cookie的。 我们可以在响应中设置cookie的domain为其他域,但是浏览器并不会去保存这些domain为其他域的cookie。

2.cookie中的path能够进一步的控制cookie的访问,当path=/; 当前域的所有请求都可以访问到这个cookie。 如果path设为其他值,比如path=/test,那么只有/test下面的请求可以访问到这个cookie。

纸上得来终觉浅,绝知此事要躬行。

首先我们来看看默认情况下的cookie的domain和path是什么。服务端使用servlet。

//这里故意将url设置的特别长,方便观察path的默认值
@WebServlet("/test/test1/test2.html")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie nameCookie = new Cookie("name", "zhangsan");
response.addCookie(nameCookie); try(PrintWriter out = response.getWriter()){ out.print("set name cookie"); }
}
}

上面的cookie没有任何特别设置,只有基本的名和值。响应的头信息中也是最基本的情况,如下图:

我们现在可以看看浏览器中保存了什么了。

我们从浏览器的保存情况中可以看出: 1. 浏览器是按照domain域来组织cookie的。

                  2. domain的默认值为请求的域名。

                  3. path的默认值为请求的上一层目录(请求为:/hello/test/test1/test2.html, path为/hello/test/test1)

更改cookie的domain和path值

我们可以改变cookie的domain和path的值,看看会发生什么。

@WebServlet("/test/test1/test2.html")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie nameCookie = new Cookie("name", "zhangsan");
nameCookie.setDomain("www.example2.com");//domain 更改为其他的域名
response.addCookie(nameCookie); Cookie ageCookie = new Cookie("age", "11");
ageCookie.setPath("/"); //path 更改为根目录
response.addCookie(ageCookie); try(PrintWriter out = response.getWriter()){ out.print("set name and age cookies"); }
}
}

上面的代码中我们返回了两个cookie,nameCookie更改了domain,ageCookie更改了path,我们看看会发生些什么。

首先还是看看响应的头信息:

我们在代码中的改动完全反映到了响应的头信息中,但是浏览器的保存结果可能和你想象的不太一样。如下图:

我们看到浏览器仅仅保存了ageCookie,并没有保存nameCookie,实际上就是因为浏览器不会从一个响应中保存其他域名的cookie。

在网上看到一些关于更改domain的文章,包括使用js来实现的,但是我实践的结果都是不可以的。

path控制cookie的访问

我们不仅无法访问到其他域的cookie,我们同样无法访问到当前请求的url不在path属性之下的cookie。

@WebServlet("/test/test1/test2.html")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie nameCookie = new Cookie("name", "zhangsan");
response.addCookie(nameCookie); Cookie ageCookie = new Cookie("age", "11");
ageCookie.setPath("/"); //path 更改为根目录
response.addCookie(ageCookie); try(PrintWriter out = response.getWriter()){ out.print("set name and age cookies"); }
}
}

上面的代码中nameCookie使用默认path,也就是 /hello/test/test1, ageCookie使用根目录作为path。 我们从两个不同的路径来访问cookie,代码如下:

//@WebServlet("/test/test2/access.html")
@WebServlet("/test/test1/access.html")
public class AccessServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try(PrintWriter out = response.getWriter();){
Cookie[] cs = request.getCookies();
if(cs != null){
for(Cookie c : cs){
out.print(c.getName() + " --> " + c.getValue());
}
}
}
}
}

分别从两个url来访问,结果/test/test2/access.html仅仅能访问到age,而/test/test1/access.html能够访问到age和name。

注意:在Apache Http Server 反向代理中,如果我们不去更改cookie中domain和path的值,就有可能出现 浏览器根本不会去保存这个cookie或者即使保存了也无法正常访问的情况。

cookie中的domain和path的更多相关文章

  1. cookie中的path与domain属性详解

    1.domain表示的是cookie所在的域,默认为请求的地址,如网址为www.jb51.net/test/test.aspx,那么domain默认为www.jb51.net.而跨域访问,如域A为t1 ...

  2. js与cookie的domain和path之间的关系

    1.前言 使用javascript操作cookie我们都经常使用,对cookie不是很了解的话可以看下这篇帖子[javascript操作cookie](http://www.cnblogs.com/D ...

  3. js获取cookie中存储的值

    最近看了试卷题目发现自己会的十分的匮乏, 第一题就把自己难住了,知道有这个东西,但是实际上没有操作过. ========================================= cookie ...

  4. IOS - 打印COOKIE中的 CRFSToken

    NSHTTPCookie 在iOS中使用NSHTTPCookie类封装一条cookie,通过NSHTTPCookie的方法读取到cookie的通用属性. - (NSUInteger)version; ...

  5. jquery.cookie中的操作

    http://w3school.com.cn/js/js_cookies.asp jquery.cookie中的操作: jquery.cookie.js是一个基于jquery的插件,点击下载! 创建一 ...

  6. Cookie中的HttpOnly详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt377 1.什么是HttpOnly? 如果您在cookie中设置了HttpOn ...

  7. (转)jquery.cookie中的操作

      jquery.cookie中的操作: jquery.cookie.js是一个基于jquery的插件,点击下载! 创建一个会话cookie: $.cookie(‘cookieName’,'cooki ...

  8. Cookie中存放数据l加密解密的算法

    public class CookieUtil { /** * * @param response HttpServletResponse类型的响应 * @param cookie 要设置httpOn ...

  9. Appscan漏洞 之 加密会话(SSL)Cookie 中缺少 Secure 属性

    近期 Appscan扫描出漏洞 加密会话(SSL)Cookie 中缺少 Secure 属性,已做修复,现进行总结如下: 1.1.攻击原理 任何以明文形式发送到服务器的 cookie.会话令牌或用户凭证 ...

随机推荐

  1. 面向切面@Aspect

    package com.imooc.demo.filter; import org.springframework.core.Ordered; import org.springframework.c ...

  2. [永恒之黑]CVE-2020-0796(漏洞复现)

    实验环境: 靶机:windows10 1903 专业版 攻击机:kali 2020.3 VMware:vmware14 实验工具: Python 3.8.5   msfconsole 实验PROC: ...

  3. 设计模式——从HttpServletRequestWrapper了解装饰者模式

    从一个业务开始 最近项目上紧急需要,为了应付一个不知道啥的安全检测,我们要给系统追加防XSS注入的功能,这里有经验的JavaWeb开发就会想到,用过滤器或者基于项目框架的拦截器来做,但是顺着这个思路下 ...

  4. Redis的批量操作是什么?怎么实现的延时队列?以及订阅模式、LRU。

    前言 这次的内容是我自己为了总结Redis知识而扩充的,上一篇其实已经总结了几点知识了,但是Redis的强大,以及适用范围之广可不是单单一篇博文就能总结清的.所以这次准备继续总结,因为第一个问题,Re ...

  5. Beta冲刺——第十天(补发)

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE1 这个作业要求在哪里 https://edu.cnblogs.com/campus/fz ...

  6. Head First 设计模式 —— 02. 观察者 (Observer) 模式

    思考题 在我们的一个实现中,下列哪种说法正确?(多选) P42 public class WeatherDate { // 实例变量声明 public void measurementsChanged ...

  7. 关于一些视图的基本操作(结合YGGL.sql)

    二.操作题 1.创建视图emp_view2,包含员工编号,姓名,所在部门名称和收入. mysql> create or replace view emp_view2 -> as -> ...

  8. 消息队列 ---常用的 MQ 中间件

    目前市面上比较常用的 MQ(Message Queue,消息队列)中间件有 RabbitMQ.Kafka.RocketMQ,如果是轻量级的消息队列可以使用 Redis 提供的消息队列,其中 Redis ...

  9. #2使用html+css+js制作网站教程 测试

    #2使用html+css+js制作网站教程 测试 本系列链接 1 测试 1.1 运行 1.2 审查 1.3 审查技巧 1.4 其他 引言: 编写完代码后就要上机测试代码,获得用户体验,筛选bug 笔者 ...

  10. 解决GitHub下载速度慢的问题(已解决)

    核心:通过码云导入github资源,通过码云转接下载. $\color{blue}{1. 找到需要下载的GitHub地址}$  ​ 然后复制链接,转到码云上去. $\color{blue}{2. 打开 ...