cookie中的domain和path
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。
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。
cookie中的domain和path的更多相关文章
- cookie中的path与domain属性详解
1.domain表示的是cookie所在的域,默认为请求的地址,如网址为www.jb51.net/test/test.aspx,那么domain默认为www.jb51.net.而跨域访问,如域A为t1 ...
- js与cookie的domain和path之间的关系
1.前言 使用javascript操作cookie我们都经常使用,对cookie不是很了解的话可以看下这篇帖子[javascript操作cookie](http://www.cnblogs.com/D ...
- js获取cookie中存储的值
最近看了试卷题目发现自己会的十分的匮乏, 第一题就把自己难住了,知道有这个东西,但是实际上没有操作过. ========================================= cookie ...
- IOS - 打印COOKIE中的 CRFSToken
NSHTTPCookie 在iOS中使用NSHTTPCookie类封装一条cookie,通过NSHTTPCookie的方法读取到cookie的通用属性. - (NSUInteger)version; ...
- jquery.cookie中的操作
http://w3school.com.cn/js/js_cookies.asp jquery.cookie中的操作: jquery.cookie.js是一个基于jquery的插件,点击下载! 创建一 ...
- Cookie中的HttpOnly详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt377 1.什么是HttpOnly? 如果您在cookie中设置了HttpOn ...
- (转)jquery.cookie中的操作
jquery.cookie中的操作: jquery.cookie.js是一个基于jquery的插件,点击下载! 创建一个会话cookie: $.cookie(‘cookieName’,'cooki ...
- Cookie中存放数据l加密解密的算法
public class CookieUtil { /** * * @param response HttpServletResponse类型的响应 * @param cookie 要设置httpOn ...
- Appscan漏洞 之 加密会话(SSL)Cookie 中缺少 Secure 属性
近期 Appscan扫描出漏洞 加密会话(SSL)Cookie 中缺少 Secure 属性,已做修复,现进行总结如下: 1.1.攻击原理 任何以明文形式发送到服务器的 cookie.会话令牌或用户凭证 ...
随机推荐
- 《HelloGitHub》第 57 期
兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程.对开源社区感兴趣 人群的月刊,月刊的内容包括:各种编 ...
- easyui中刷新列表
<table class="crud-content-info" id="showProductDialogFormstandrad"> </ ...
- 新下载的Chrome 不能用,设置搜索引擎,谷歌浏览器不能用,chrome浏览器不能用,google chrome 不能用
新下载的chrome默认搜索引擎 是google搜索,而google搜索引擎在国内是不能使用的,要设置为 百度或.360.搜狗搜索引擎才能使用. 设置方法如下: 1.打开 Chrome. 2.点击右上 ...
- java函数方法学习
1.函数(方法)定义 类中特定功能小程序 2.函数定义格式 修饰符 返回值类型 函数名 (参数类型 形式参数) { 执行语句; return 返回值 } 函数功能实现的2个明确 1.这个功能的结果是什 ...
- 在 Emit 代码中如何await一个异步方法
0. 前言 首先立马解释一波为啥会有这样一篇伪标题的Demo随笔呢? 不是本人有知识误区,或者要误人子弟 因为大家都知道emit写出来的都是同步方法,不可能await,至少现在这么多年来没有提供对应的 ...
- 30天自制操作系统-day3
30天自制操作系统-day3 前2天我们分别使用了直接使用二进制编辑器和简单的汇编指令生成了img文件,今天我们尝试一下使用稍微复杂一点的汇编指令 os.asm文件内容如下: ; hello-os ; ...
- 第二章 进程同步(二)——> 重点
2.4 进程同步 2.4.1 进程同步的基本概念 1. 两种形式的制约关系 (1)间接相互制约关系:互斥问题(往往是互斥设备)---是同步的特例 (2)直接相互制约关系:同步问题 注: 互斥问题 ...
- Kafka基本原理概述
Kafka的基本介绍 Kafka是最初由Linkedin公司开发,是一个分布式.分区的.多副本的.多订阅者,基于zookeeper协调的分布式日志系统(也可以当做MQ系统),常见可以用于web/ngi ...
- linux系统修改不成功无法修改密码
一.问题描述 新上架的浪潮服务器使用装机平台进行统一安装,安装完成后修改用户密码时统一无法修改,使用root账户无法修改其他用户密码,自身根密码也无法修改成功,报错如下 Changing passwo ...
- 一些php文件函数
当读入一个巨大的字符串的时候不能使用file_get_contents('文件名') 应该 使用fopen('文件名','r') feof('文件名') //判断是否读到了文件结尾 ******** ...