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.会话令牌或用户凭证 ...
随机推荐
- Adnroid 源码学习笔记:Handler 线程间通讯
常见的使用Handler线程间通讯: 主线程: Handler handler = new Handler() { @Override public void handleMessage(Messag ...
- java中远程调用接口springboot
package com.kakarote.crm.utils; import cn.hutool.core.util.ObjectUtil; import org.apache.http.client ...
- 读取 excel文件组装字典数据
package com.murong.ecp.app.mbu.action.bmbuurm8; import java.io.FileOutputStream;import java.io.Outpu ...
- 拖拽一个元素如此简单,mouse、drag、touch三兄弟的用处
最近需要做一个投票活动,上传图片时需要拖拽.缩放来裁剪图片,vue的组件不少,不过自己动手才能丰衣足食,一味使用别人的组件实在难以进步,所以自己研究一番. 一.mouse.drag.touch傻傻分不 ...
- SQL数据库创建,创建表,增删改查
创建数据库:create datebase数据库名 删除数据库:drop datebase 数据库名称 创建表格式: create table 表名(字段名1,字段类型1,字段名2,字段类型2) 查询 ...
- 异步技巧之CompletableFuture
摘自--https://juejin.im/post/5b4622df5188251ac9766f47 异步技巧之CompletableFuture 1.Future接口 1.1 什么是Future? ...
- 一台PC端安装多店仓信息的删除
如图所示,安装了多店仓,想要删除其中莫一店仓信息! 步骤一:先找到对应目录,一般默认的目录为C:\Users\xxxx\AppData\Roaming\WebPos2.0\bosnatweiniman ...
- 如何根据角色批量激活SAP Fiori服务
我们知道Fiori的角色跟ERP的角色是不通用的,即使你的账号有SAP_ALL的权限,但打开Fiori的时候一样是空的一片: 只有给账号加上fiori需要的角色,并激活相关服务才能用fiori app ...
- jq 右键菜单在弹出菜单前如果需要显示与否的判断相关操作
菜单插件(ContextMenu)接收一个额外的参数对象来设置菜单项的样式和绑定鼠标事件. 菜单插件(ContextMenu)支持一下参数设置: bindings 包含id的对象:函数组. 当关联的菜 ...
- 「译」 .NET 5 新增的Http, Sockets, DNS 和 TLS 遥测
.NET 一直在稳定的增加和改善对应用程序进行跨平台的诊断分析,在.NET Core 3.0, 我们看到了 EventCounters 的介绍,用于观察和分析指标测量. 我最近在几个 .NET Cor ...