`
中国爪哇程序员
  • 浏览: 164754 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Cookie 浅谈

    博客分类:
  • java
 
阅读更多
随记Cookie

先看源码

/**
 *
 * Creates a cookie, a small amount of information sent by a servlet to 
 * a Web browser, saved by the browser, and later sent back to the server.
 * A cookie's value can uniquely 
 * identify a client, so cookies are commonly used for session management.
 * 
 * <p>A cookie has a name, a single value, and optional attributes
 * such as a comment, path and domain qualifiers, a maximum age, and a
 * version number. Some Web browsers have bugs in how they handle the 
 * optional attributes, so use them sparingly to improve the interoperability 
 * of your servlets.
 *
 * <p>The servlet sends cookies to the browser by using the
 * {@link HttpServletResponse#addCookie} method, which adds
 * fields to HTTP response headers to send cookies to the 
 * browser, one at a time. The browser is expected to 
 * support 20 cookies for each Web server, 300 cookies total, and
 * may limit cookie size to 4 KB each.
 * 
 * <p>The browser returns cookies to the servlet by adding 
 * fields to HTTP request headers. Cookies can be retrieved
 * from a request by using the {@link HttpServletRequest#getCookies} method.
 * Several cookies might have the same name but different path attributes.
 * 
 * <p>Cookies affect the caching of the Web pages that use them. 
 * HTTP 1.0 does not cache pages that use cookies created with
 * this class. This class does not support the cache control
 * defined with HTTP 1.1.
 *
 * <p>This class supports both the Version 0 (by Netscape) and Version 1 
 * (by RFC 2109) cookie specifications. By default, cookies are
 * created using Version 0 to ensure the best interoperability.
 *
 *
 * @author	Various
 */

// XXX would implement java.io.Serializable too, but can't do that
// so long as sun.servlet.* must run on older JDK 1.02 JVMs which
// don't include that support.

public class Cookie implements Cloneable {

    private static final String LSTRING_FILE =
	"javax.servlet.http.LocalStrings";
    private static ResourceBundle lStrings =
	ResourceBundle.getBundle(LSTRING_FILE);
    
    //
    // The value of the cookie itself.
    //
    
    private String name;	// NAME= ... "$Name" style is reserved
    private String value;	// value of NAME

    //
    // Attributes encoded in the header's cookie fields.
    //
    
    private String comment;	// ;Comment=VALUE ... describes cookie's use
				// ;Discard ... implied by maxAge < 0
    private String domain;	// ;Domain=VALUE ... domain that sees cookie
    private int maxAge = -1;	// ;Max-Age=VALUE ... cookies auto-expire
    private String path;	// ;Path=VALUE ... URLs that see the cookie
    private boolean secure;	// ;Secure ... e.g. use SSL
    private int version = 0;	// ;Version=1 ... means RFC 2109++ style
    
    


第一 与Session的比较
十年前还单机处理业务,集群还不多的时候,Session用的还比较多,现在随便个服务都集群部署,考虑到多节点内存同步,都不太使用session。http是无状态的,保留用户信息用,采用session会话。网上资料太多,不再赘述。

第二 API
太简单,不再赘述

第二 属性信息
属性:name value
有人把cookie理解成map,name 相当key, value 相当 map里的value.
但本身Cookie是个数组。是个Cookie[]
所以这个key是可以重复的。但又经常把cookie当成map使用,所以建议把cookie操作封装下。

属性 comment
就是存储key value 的描述。没什么特别的。

属性 maxAge
cookie的生命周期,默认-1,即关闭浏览器,cookie失效。
单位是??,大于零,即使cookie关闭,cookie依然生效。

属性 version
int ASSIC 数值,准照RFC 标准。
RFC文件是纯ASCII文字档格式
RFC https://zh.wikipedia.org/wiki/RFC#RFC.E6.96.87.E4.BB.B6.E7.9A.84.E6.9E.B6.E6.A7.8B

属性 path
不瞎逼逼了,附上源码描述把
  
  * Specifies a path for the cookie
     * to which the client should return the cookie.
     *
     * <p>The cookie is visible to all the pages in the directory
     * you specify, and all the pages in that directory's subdirectories. 
     * A cookie's path must include the servlet that set the cookie,
     * for example, <i>/catalog</i>, which makes the cookie
     * visible to all directories on the server under <i>/catalog</i>.
     *
     * <p>Consult RFC 2109 (available on the Internet) for more
     * information on setting path names for cookies.


最后讲的属性 domain
参考文档:http://blog.csdn.net/alexxu1988/article/details/47805205

     * Specifies the domain within which this cookie should be presented.
     *
     * <p>The form of the domain name is specified by RFC 2109. A domain
     * name begins with a dot (<code>.foo.com</code>) and means that
     * the cookie is visible to servers in a specified Domain Name System
     * (DNS) zone (for example, <code>www.foo.com</code>, but not 
     * <code>a.b.foo.com</code>). By default, cookies are only returned
     * to the server that sent them.


domain的知识点比较多。

最后一点
cookie 是不安全的
cookie是可以篡改,模拟的。因为是在客户端,之前我本地模拟了A站点的cookie, 用这个cookie是可以直接供真正的A站点使用的。那为什么还要用cookie.方便呀。
建议cookie存放的信息不是敏感信息,像密码这类东西就不要考虑放到cookie.存放的token 后台也要加个校验。cookie攻击的技术门槛是很低的。

分享到:
评论

相关推荐

    在PHP中浅谈Cookie与Session.pdf

    在PHP中浅谈Cookie与Session.pdf

    JavaScript基础篇——浅谈cookie

    cookie,简单的理解。就是存储数据。通过cookie,数据可以长期保存,不随着浏览器的关闭而消失。 本文有助于初学者加深对cookie的理解,个人总结,不足之处欢迎随时交流。

    浅谈COOKIE和SESSION区别

    一、cookie介绍 cookie 常用于识别用户。cookie 是服务器留在用户计算机中的小文件。每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie。通过 PHP,您能够创建并取回 cookie 的值。 1、设置Cookie PHP用...

    浅谈Cookie的生命周期问题

    下面小编就为大家带来一篇浅谈Cookie的生命周期问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    浅谈PHP Cookie处理函数

    下面小编就为大家带来一篇浅谈PHP Cookie处理函数。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    浅谈golang的http cookie用法

    在服务端程序开发的过程中,cookie经常被用于验证用户登录。golang 的 net/http 包中自带 http cookie的定义,下面就来讲一下cookie的一般用法以及需要注意的问题。 http cookie的定义 先来看下golang对cookie结构体...

    浅谈cookie 和session 的区别

    下面小编就为大家带来一篇浅谈cookie 和session 的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    浅谈cookie和session(小结)

    主要介绍了浅谈cookie和session(小结),cookie和session在java web开发中扮演了十分重要的作用,本篇文章对其中的重要知识点做一些探究和总结

    浅谈cookie和localStorage那些事

    一、localStorage、cookie、sessionStorage的区别与练习 1、cookie 小甜饼。它的大小限制为4KB左右,是网景公司的前雇员 Lou Montulli 在1993年3月的发明。它的主要用途有保存登录信息,比如你登录某个网站市场可以...

    padding oracle攻击浅谈

    padding oracle攻击详解。

    浅谈网络安全防护技术.doc

    浅谈网络安全防护技术 摘要:随着计算机网络应用的广泛深入.网络安全问题变得日益复杂和突出。目前计 算机病毒技术和黑客技术的融合,使得计算机所受到的威胁更加难以预料。本文从常见 的网络安全防护方法入手,...

    浅谈php(codeigniter)安全性注意事项

    session一定要用httponly的否则可能被xxs攻击,利用js获取cookie的session_id。 要用框架的ci_session,更长的位数,httponly,这些默认都配好了。 不要用原生的phpsession,而要用ci_session。ci_session位数更长。...

    浅谈css sticker-footer 布局

    本篇文章主要介绍了css sticker-footer 布局,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    浅谈Vue SSR 的 Cookies 问题

    一个网站一旦涉及到多用户, 就很难从 Cookies 中逃脱, Vue SSR 的 cookies 也真算是遇到的一个不小的问题, 从开始玩 SSR 开始到现在, 一共想出了3种方案, 从最早的把 Cookies 注入到 state 中, 到把 Cookies 注入到 ...

    浅谈C#中HttpWebRequest与HttpWebResponse的使用方法

    C# HttpHelper,帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取 1.第一招,根据URL地址获取网页信息 先来看一下代码 get方法 public static string GetUrltoHtml(string Url,string ...

    浅谈关于axios和session的一些事

    一个登录的场景,用axios发送post请求去登录,能成功返回数据,但是用作权限验证的cookie就是没有保存,经查阅,axios 默认不发送cookie,跨域也是一个原因,需要全局设置 所以我们需要这么设置 axios.defaults....

Global site tag (gtag.js) - Google Analytics