• Java 设置 httponly cookie


    Httponly cookie 是一种 cookie 安全解决方案。

    在支持httponly cookie的浏览器(IE6+、FF3.0+)中,如果cookie中设置了“httponly”属性,则JavaScript脚本将无法读取cookie信息,可以有效防止XSS攻击,让网站应用更安全。

     

    但是J2EE4、J2EE5 cookie不提供设置httponly属性的方法,所以如果需要设置httponly属性需要自己处理。

    1. import javax.servlet.http.Cookie;
    2. import javax.servlet.http.HttpServletResponse;
    3. /**
    4. * Cookie Tools
    5. */
    6. public class CookieUtil {
    7. /**
    8. * Set httponly cookie
    9. * @param Response HTTP response
    10. * @param Cookie cookie object
    11. * @param Ishttponly is httponly
    12. */
    13. public static void addCookie(HttpServletResponse response, Cookie cookie, boolean isHttpOnly) {
    14. String name = cookie.getName();//Cookie name
    15. String value = cookie.getValue();//Cookie value
    16. int maxAge = cookie.getMaxAge();//Maximum survival time (milliseconds, 0 representative deletion, -1 represents the same as the browser session)
    17. String path = cookie.getPath();//path
    18. String domain = cookie.getDomain();//area
    19. boolean isSecure = cookie.getSecure();//Is there a security protocol?
    20. StringBuilder buffer = new StringBuilder();
    21. buffer.append(name).append("=").append(value).append(";");
    22. if (maxAge == 0) {
    23. buffer.append("Expires=Thu Jan 01 08:00:00 CST 1970;");
    24. } else if (maxAge > 0) {
    25. buffer.append("Max-Age=").append(maxAge).append(";");
    26. }
    27. if (domain != null) {
    28. buffer.append("domain=").append(domain).append(";");
    29. }
    30. if (path != null) {
    31. buffer.append("path=").append(path).append(";");
    32. }
    33. if (isSecure) {
    34. buffer.append("secure;");
    35. }
    36. if (isHttpOnly) {
    37. buffer.append("HTTPOnly;");
    38. }
    39. response.addHeader("Set-Cookie", buffer.toString());
    40. }
    41. }

    值得一提的是,Java Ee 6.0中的cookie已经设置了httponly,所以如果兼容Java EE 6.0兼容的容器(例如Tomcat 7),可以使用cookie.sethttponly设置HTTPONLY:

    cookie.setHttpOnly(true);

    Java HttpCookie 类的setHttpOnly(Boolean httpOnly) 方法用于指示cookie 是否可以被认为是HTTPOnly。如果设置为 true,则 cookie 不能被 JavaScript 等脚本引擎访问。

    句法

    public void setHttpOnly(boolean httpOnly)  
    

    范围

    上述方法只需要一个参数:

    1. httpOnly - 如果 cookie 仅是 HTTP,则表示 true,这意味着它作为 HTTP 请求的一部分可见。

    返回

    不适用

    示例 1

    1. import java.net.HttpCookie;
    2. public class JavaHttpCookieSetHttpOnlyExample1 {
    3. public static void main(String[] args) {
    4. HttpCookie cookie = new HttpCookie("Student", "1");
    5. // Indicate whether the cookie can be considered as HTTP Only or not.
    6. cookie.setHttpOnly(true);
    7. // Return true if the cookie is considered as HTTPOnly.
    8. System.out.println("Check whether the cookie is HTTPOnly: "+cookie.isHttpOnly());
    9. }
    10. }

    输出:

    Check whether the cookie is HTTPOnly: true
    

    示例 2

    1. import java.net.HttpCookie;
    2. public class JavaHttpCookieSetHttpOnlyExample2 {
    3. public static void main(String[] args) {
    4. HttpCookie cookie = new HttpCookie("Student", "1");
    5. // Indicate whether the cookie can be considered as HTTP Only or not.
    6. cookie.setHttpOnly(false);
    7. // Return false if the cookie is not considered as HTTPOnly.
    8. System.out.println("Check whether the cookie is HTTPOnly: "+cookie.isHttpOnly());
    9. }
    10. }

    输出:

    Check whether the cookie is HTTPOnly: false
    

    示例 3

    1. import java.net.HttpCookie;
    2. public class JavaHttpCookieSetHttpOnlyExample3 {
    3. public static void main(String[] args) {
    4. HttpCookie cookie1 = new HttpCookie("Student1", "1");
    5. HttpCookie cookie2 = new HttpCookie("Student2", "2");
    6. //Indicate whether the cookie can be considered as HTTP Only or not.
    7. cookie1.setHttpOnly(true);
    8. cookie2.setHttpOnly(false);
    9. System.out.println("Check whether the first cookie is HTTPOnly:"+cookie1.isHttpOnly());
    10. System.out.println("Check whether the second cookie is HTTPOnly:"+cookie2.isHttpOnly());
    11. }
    12. }

    输出:

    1. Check whether the first cookie is HTTPOnly:true
    2. Check whether the second cookie is HTTPOnly:false

  • 相关阅读:
    苹果Mac电脑L2TP连接公司内部网络失败解决方案
    Himall类型帮助类将string类型转换成decimal类型
    前端三刺客---CSS
    11个程序员必备简捷开发辅助工具
    C语言——扫雷游戏最全讲解
    着手开发属于自己的第一个Intellij-platform plugin插件程序(三)
    【服务器】Java连接redis及使用Java操作redis、使用场景
    OpenMPI的安装与运行分布式项目
    从浏览器输入url到页面加载(五)请求数据在网线中的故事
    基于arm-linux-gcc版本,音乐视频播放器mplayer
  • 原文地址:https://blog.csdn.net/allway2/article/details/126121017