cookie中的domain和path

div.example { background-color: #e5ecf3; color: #000; padding: 0.5em; margin: 1em 2em 1em 1em }
div.warning { border: 1px solid #f00 }

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或者即使保存了也无法正常访问的情况。

时间: 2024-07-29 07:53:04

cookie中的domain和path的相关文章

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

1.前言 使用javascript操作cookie我们都经常使用,对cookie不是很了解的话可以看下这篇帖子[javascript操作cookie](http://www.cnblogs.com/Darren_code/archive/2011/11/24/Cookie.html "javascript操作cookie"):常用的cookie知识点上面那位大神已经讲完了,有点点小补充顺便说下,不对的地方欢迎吐槽. 2.外部js 做网站经常要引入其他网站的js.一直以来我以为引入的js

jquery.cookie中的操作

jquery.cookie中的操作: jquery.cookie.js是一个基于jquery的插件,点击下载! 创建一个会话cookie: $.cookie(‘cookieName’,'cookieValue’); 注:当没有指明cookie时间时,所创建的cookie有效期默认到用户浏览器关闭止,故被称为会话cookie. 创建一个持久cookie: $.cookie(‘cookieName’,'cookieValue’,{expires:7}); 注:当指明时间时,故称为持久cookie,并

Cookie中的HttpOnly详解

详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt377 1.什么是HttpOnly? 如果您在cookie中设置了HttpOnly属性,那么通过js脚本将无法读取到cookie信息,这样能有效的防止XSS攻击,具体一点的介绍请google进行搜索 2.javaEE的API是否支持? 目前sun公司还没有公布相关的API,但PHP.C#均有实现.搞javaEE的兄弟们比较郁闷了,别急下文有变通实现 3.HttpOnly的设置

Cookie中设置了"HttpOnly"属性,有效的防止XSS攻击

1.什么是HttpOnly? 如果cookie中设置了HttpOnly属性,那么通过js脚本将无法读取到cookie信息,这样能有效的防止XSS攻击,窃取cookie内容,这样就增加了cookie的安全性,即便是这样,也不要将重要信息存入cookie. XSS全称Cross SiteScript,跨站脚本攻击,是Web程序中常见的漏洞,XSS属于被动式且用于客户端的攻击方式,所以容易被忽略其危害性.其原理是攻击者向有XSS漏洞的网站中输入(传入)恶意的HTML代码,当其它用户浏览该网站时,这段H

Cookie中图片的浏览记录与cookie读取servle时路径的设置(文字描述)

public class ShowServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletR

Loadrunner在post请求的cookie中插入其它多个值

有一个商城,提交订单的时候,需要在post请求的cookie中set一个code码,便于防刷 1 tijiao() 2 { 3 4 lr_think_time(1); 5 6 web_reg_save_param("validcode", 7 "LB=validcode=", 8 "RB=;", 9 "Search=All", 10 LAST); 11 12 13 14 web_submit_data("toSubm

对GET/POST请求返回cookie中的键值对进行重新组合

get/post请求返回的cookie中并不是所有的键值对我们都需要,我们只需要提取我们需要的进行重新组合就可以了. 如下图是一个GET请求返回的cookie 我需要提取其中的 uin,skey等相关键值对. 以下函数可以完成我们的需要: //using System.Text.RegularExpressions; public string GetCookieByName(List<string> keylist, string cookie) { string str = "&

cookie中存中文

cookie中存中文 1:想要在cookie中存中文:需要用到URLEncoder(在jdkAPI中有介绍) Cookie cookie = new Cookie("User",URLEncoder.encode(要编码的参数,"utf-8"));(编码后页面需要解码) 2:页面解码: 用js 解码. w3c菜鸟教材中  EncoderURL可以完成解码操作: 事例代码:<script type="text/javascript">w

cookie中maxAge总是-1和tomcat设置编码方式

指定tomcat的URI编码方法 tomcat中conf文件夹下修改server.xml文件,在connector属性中添加语句 URIEncoding="UTF-8" useBodyEncodingForURI="true" 利用cookie存储了登录名loinname字段并设置maxAge为2天,结果再次访问localhost:8080/a/1.do时.在相对于的servlet的request中,reques.getCookie()可以拿到相应的cookie,但