Java设置httponly cookie的实现示例

 更新时间:2022年08月03日 09:42:52   作者:allway2  
本文主要介绍了Java设置httponly cookie的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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

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

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

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Cookie Tools
 */
public class CookieUtil {
 
    /**
           * Set httponly cookie
     * @param  Response HTTP response
     * @param  Cookie cookie object
     * @param  Ishttponly is httponly
     */
    public static void addCookie(HttpServletResponse response, Cookie cookie, boolean isHttpOnly) {
        String name = cookie.getName();//Cookie name
        String value = cookie.getValue();//Cookie value
        int maxAge = cookie.getMaxAge();//Maximum survival time (milliseconds, 0 representative deletion, -1 represents the same as the browser session)
        String path = cookie.getPath();//path
        String domain = cookie.getDomain();//area
        boolean isSecure = cookie.getSecure();//Is there a security protocol? 
 
        StringBuilder buffer = new StringBuilder();
 
        buffer.append(name).append("=").append(value).append(";");
 
        if (maxAge == 0) {
            buffer.append("Expires=Thu Jan 01 08:00:00 CST 1970;");
        } else if (maxAge > 0) {
            buffer.append("Max-Age=").append(maxAge).append(";");
        }
 
        if (domain != null) {
            buffer.append("domain=").append(domain).append(";");
        }
 
        if (path != null) {
            buffer.append("path=").append(path).append(";");
        }
 
        if (isSecure) {
            buffer.append("secure;");
        }
 
        if (isHttpOnly) {
            buffer.append("HTTPOnly;");
        }
 
        response.addHeader("Set-Cookie", buffer.toString());
    }
 
}

值得一提的是,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)  

范围

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

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

返回

不适用

示例 1

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

输出:

Check whether the cookie is HTTPOnly: true

示例 2

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

输出:

Check whether the cookie is HTTPOnly: false

示例 3

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

输出:

Check whether the first cookie is HTTPOnly:true
Check whether the second cookie is HTTPOnly:false

到此这篇关于Java设置httponly cookie的实现示例的文章就介绍到这了,更多相关Java设置httponly cookie内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:

相关文章

  • JAVA IO的3种类型区别解析

    JAVA IO的3种类型区别解析

    这篇文章主要介绍了JAVA IO的3种类型解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • SpringBoot中的CompletableFuture类详解

    SpringBoot中的CompletableFuture类详解

    这篇文章主要介绍了SpringBoot中的CompletableFuture类详解,在 Java8中,引入了CompletableFuture类,它提供了一种简单而强大的方式来执行异步任务,今天我们就来详细解读一下这个类,需要的朋友可以参考下
    2023-07-07
  • Java concurrency线程池之线程池原理(二)_动力节点Java学院整理

    Java concurrency线程池之线程池原理(二)_动力节点Java学院整理

    这篇文章主要为大家详细介绍了Java concurrency线程池之线程池原理第二篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • SpringBoot启动参数的实现

    SpringBoot启动参数的实现

    SpringBoot通过jar文件方式启动,配置可以通过启动参数进行覆盖,本文就来介绍一下SpringBoot启动参数的实现,感兴趣的可以了解一下
    2025-01-01
  • Spring中的@Autowired、@Qualifier和@Primary注解详解

    Spring中的@Autowired、@Qualifier和@Primary注解详解

    这篇文章主要介绍了Spring中的@Autowired、@Qualifier和@Primary注解详解,@Autowired 注解,可以对类成员变量、方法和构造函数进行标注,完成自动装配的工作,@Autowired 是默认根据 byType 进行自动装配的,需要的朋友可以参考下
    2023-11-11
  • 如何通过JVM角度谈谈Java的clone操作

    如何通过JVM角度谈谈Java的clone操作

    java中仅有的创建对象的两种方式:①.使用new操作符创建对象;②.使用clone方法复制对象。下面这篇文章主要通过JVM角度给大家详细谈谈Java的clone操作的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-06-06
  • Springboot中PropertySource的结构与加载过程逐步分析讲解

    Springboot中PropertySource的结构与加载过程逐步分析讲解

    本文重点讲解一下Spring中@PropertySource注解的使用,PropertySource主要是对属性源的抽象,包含属性源名称name和属性源内容对象source。其方法主要是对这两个字段进行操作
    2023-01-01
  • java最新版本连接mysql失败的解决过程

    java最新版本连接mysql失败的解决过程

    这篇文章主要给大家介绍了关于java最新版本连接mysql失败的解决过程,文中通过图文以及示例代码将解决的过程介绍的非常详细,对遇到这个问题的同学具有一定的参考学习价值,需要的朋友可以参考下
    2021-09-09
  • 如何在Spring Boot中使用OAuth2认证和授权

    如何在Spring Boot中使用OAuth2认证和授权

    这篇文章主要介绍了如何在Spring Boot中使用OAuth2认证和授权的相关资料,OAuth2.0是一种开放的授权协议,它允许用户授权第三方应用访问其账户(或资源),而无需共享其用户账户凭据,需要的朋友可以参考下
    2023-12-12
  • eclipse+myeclipse 环境配置方法

    eclipse+myeclipse 环境配置方法

    eclipse+myeclipse配置环境
    2009-07-07

最新评论