`
flowercat
  • 浏览: 362310 次
社区版块
存档分类
最新评论

play!存在的Cookie设置的bug

    博客分类:
  • java
阅读更多

目前playframework使用的Mina在设置Cookie Cache时 只设置了Max-age,未设置Expires,这导致Cookie的存活期在所有的IE浏览器里失效,无法实现remember me!

Max-age与Expires区别见下文:

 

http://mrcoles.com/blog/cookies-max-age-vs-expires/

HTTP Cookies: What's the difference between Max-age and Expires?

Quick Answer:

  • Expires sets an expiry date for when a cookie gets deleted
  • Max-age sets the time in seconds for when a cookie will be deleted
  • Internet Explorer (ie6, ie7, and ie8) does not support “max-age” , while (mostly) all browsers support expires

Max-age vs Expires, let’s dive in a little deeper:

The expires parameter was part of the original cookies baked up by Netscape. In HTTP version 1.1, expires was deprecated and replaced with the easier-to-use max-age —instead of having to specify a date, you can just say how long the cookie can live. By setting either of these, the cookie will persist until its time runs out, otherwise—if you set neither—the cookie will last until you close your browser.

Setting a cookie for “foo=bar” to last 5 minutes, using expires :

var d = new Date();
d.setTime(d.getTime() + 5*60*1000); // in milliseconds
document.cookie = 'foo=bar;path=/;expires='+d.toGMTString()+';';

And the same with max-age :

document.cookie = 'foo=bar;path=/;max-age='+5*60+';';

Unfortunately, none of the current versions of Internet Explorer support max-age , so if you want proper cookie persistence cross-browser, then stick to expires .

Let’s open this up to some fake Q&A…

Q. What if I set both expires and max-age in a cookie?
A. Every browser that supports max-age will ignore the expires regardless of it’s value, and likewise, Internet Explorer will ignore the max-age and just use expires .

Q. What if I set just max-age in a cookie?
A. Every browser—except Internet Explorer—uses it properly. In Internet Explorer it will be a session cookie (it will be deleted when you close your browser).

Q. What if I set just expires in a cookie?
A. Every browser uses and persists it properly, just remember to set it in GMT time as seen in the example above.

Q. Where did you get these facts from?
A. I wrote a cookie persistence test page and tested it out on IE6, IE7, IE8, FF2, FF3, Safari 4, Google Chrome, and Opera 9.6. Let me know if you try it out on any other browsers or see anything contradictory.

Q. What’s the moral of this story?
A. If you care about your cookies functioning properly for a huge percentage of web users (65.66%) , don’t persist your cookies “the right way” according to spec (max-age ), persist them the way that works (expires ).

分享到:
评论
2 楼 Arden 2010-10-16  
    /**
     * 设置cookie
     * 
     * @param response
     * @param name
     * @param value
     * @param domain (.paojiao.cn)
     * @param path(/ 或者 null)
     * @param maxAge
     */
    public static void setCookie(Http.Response response, String name, String value, String domain, String path, Integer maxAge) {
        //response.setCookie(name, value, domain, path, maxAge);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, maxAge);
        Date date = calendar.getTime();
        String expires = (new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US)).format(date);
        expires = expires.replaceAll("CST", "GMT");
        if (StringUtils.isEmpty(path)) {
            path = "";
        }
        String cookie = name + "=" + value + "; domain=" + domain + "; path=" + path + "; expires=" + expires;
        System.out.println("cookie:" + cookie);
        response.setHeader("Set-Cookie", cookie);
        //response.setHeader("Set-Cookie", "email=arden.emily@gmail.com; domain=.tujiao.com; path=/; expires=Tue, 11-Oct-2011 05:48:06 GMT");
    }


我写了个手动设置Cookie的方法,解决IE不能设置cookie的Bug.
1 楼 Arden 2010-10-16  
    /**
     * 设置cookie
     *
     * @param response
     * @param name
     * @param value
     * @param domain (.paojiao.cn)
     * @param path(/ 或者 null)
     * @param maxAge
     */
    public static void setCookie(Http.Response response, String name, String value, String domain, String path, Integer maxAge) {
        //response.setCookie(name, value, domain, path, maxAge);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, maxAge);
        Date date = calendar.getTime();
        String expires = (new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US)).format(date);
        expires = expires.replaceAll("CST", "GMT");
        if (StringUtils.isEmpty(path)) {
            path = "";
        }
        String cookie = name + "=" + value + "; domain=" + domain + "; path=" + path + "; expires=" + expires;
        System.out.println("cookie:" + cookie);
        response.setHeader("Set-Cookie", cookie);
        //response.setHeader("Set-Cookie", "email=arden.emily@gmail.com; domain=.tujiao.com; path=/; expires=Tue, 11-Oct-2011 05:48:06 GMT");
    }

我写了个手动设置Cookie的方法,解决IE不能设置cookie的Bug.

相关推荐

    Learning Play!Framework 2

    ### 学习Play! Framework 2 的核心知识点 #### 一、Play! Framework 2 概述 Play! Framework 2 是一个用于构建现代 web 应用程序的高性能、轻量级框架。它由 Java 和 Scala 支持,并且特别强调开发者的生产力。...

    play framework api,play! framework api,play api

    在描述中提到的"play framework api,play! framework api,play api"都是指Play Framework的API文档,它包含了框架的所有公共类、方法和接口,供开发者在编写代码时查阅和引用。API文档是理解框架工作原理、学习如何...

    play!框架学习文档(汉化了一部分)

    ### Play! 框架简介 Play! 是一款专为高效开发企业级 Java 应用程序而设计的轻量级框架。它强调敏捷开发、快速迭代,并且支持 RESTful 架构,使得开发者能够轻松地构建现代化的 Web 应用。 #### Play! 的特点 - **...

    play!framework框架——japid源码

    **Play! Framework框架与Japid源码解析** 在软件开发领域,使用高效的框架可以极大地提升开发效率和代码质量。Play! Framework是一个流行的Java Web应用程序框架,它采用模型-视图-控制器(MVC)架构模式,支持敏捷...

    对play!的CRUD的一次改造

    对play!的CRUD 进行改造,改代码还会持续重构,并不完善。 1.将create,show,delete,list都改成@Util方法,可以类似 public static void show(String id){ MyCRUD.show(id); } 的方式调用。更通用。 2.增加@...

    大众866C升级0478最新固件 原版固件 修复各种BUG缺陷,优化主机运行速度修复倒车影像慢的问题,增加Carplay和C

    修复各种BUG缺陷,优化主机运行速度,减少主机发热,优化蓝牙链接,修复倒车影像慢的问题,增加Carplay和Carlife的稳定性,优化GPS信号 mib车机_Preh_866C/877C/682G/866BG_固件0478 适合车机:3Q0035866.C/3Q...

    play_入门学习手册

    《Play!框架入门学习手册》 Play!框架是一款全栈式的Java Web应用框架,它提供了MVC架构、对象持久化、Groovy模板引擎等一整套构建现代Web应用的工具。作为一款纯Java框架,Play!允许开发者在不改变原有开发工具和...

    play!framework_api

    一个优于RoR的快速开发框架playframework,完全面向对象,基于jvm的REST框架,文档非常少,上手很容易,从名字上可以看出play就是玩,可以当作游戏一样轻松的玩的框架,这是它的API文档,网页格式.

    让Play!Framework运行在OpenShift上

    NULL 博文链接:https://modun.iteye.com/blog/1595857

    Play Framework2本教程

    Play Framework2是一个强大的Java和Scala应用开发框架,它以其简洁的API、快速的开发周期以及对Web标准的紧密集成而闻名。本教程旨在为初学者和有经验的开发者提供全面的指导,帮助他们掌握Play Framework2的核心...

    play1.1_API

    Play!是一个full-stack(全栈的)Java Web应用框架,包括一个简单的无状态MVC模型,具有Hibernate的对象持续,一个基于Groovy的模板引擎,以及建立一个现代Web应用所需的所有东西。 Play!的关键特性: 1、一个...

    playWebsite:彼得的网站使用Play!

    Play! 框架是基于Java的一个开源Web应用框架,它以其简洁的MVC(Model-View-Controller)架构和快速开发能力而受到开发者欢迎。在这个名为"playWebsite:彼得的网站使用Play!"的项目中,我们可以推断出彼得使用了...

    屏幕录像直播器 Show Your Play!

    1 能够录像采用DirectX或OpenGL技术的3D游戏,如星际争霸2、魔兽世界、极品飞车、跑跑卡丁车、实况足球在内的主流游戏,还能录制星际争霸、祖玛、QQ游戏等各种2D游戏和经典老游戏! 2 使用Windows Media Player即可...

    2Play-v2-Themes:2播放! PlayBox v2主题

    您可以在此处检查:PlayBox v2:新的ES主题“ 2Play!系统” v1更新了 PlayBox v2:新的ES主题“ 2Play!Walls” v1更新了 PlayBox v2:新的ES主题“ 2Play!EpicMavro墙v2” PlayBox v2:新的ES主题“ 2Play!...

    carplay_ios_carplay_apple_

    4. **语音助手Siri**:Siri是CarPlay中的关键组件,它能理解自然语言指令,帮助用户执行各种任务,如发送短信、调整音量、设置导航、查询天气等,无需触碰屏幕。 5. **其他应用**:除了上述功能,CarPlay还支持播客...

    苹果carplay开发资料

    ### 知识点一:CarPlay概述 CarPlay是苹果公司推出的一种智能车载系统,它的目标是提供一种更加安全、便捷的方式来使用iPhone。通过CarPlay,用户能够将iPhone的功能与车载信息娱乐系统相结合,从而在驾驶时减少...

Global site tag (gtag.js) - Google Analytics