欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java設(shè)置httponly?cookie的實(shí)現(xiàn)示例

 更新時間:2022年08月03日 09:42:52   作者:allway2  
本文主要介紹了Java設(shè)置httponly?cookie的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

Httponly cookie 是一種 cookie 安全解決方案。

在支持httponly cookie的瀏覽器(IE6+、FF3.0+)中,如果cookie中設(shè)置了“httponly”屬性,則JavaScript腳本將無法讀取cookie信息,可以有效防止XSS攻擊,讓網(wǎng)站應(yīng)用更安全。

但是J2EE4、J2EE5 cookie不提供設(shè)置httponly屬性的方法,所以如果需要設(shè)置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已經(jīng)設(shè)置了httponly,所以如果兼容Java EE 6.0兼容的容器(例如Tomcat 7),可以使用cookie.sethttponly設(shè)置HTTPONLY:

cookie.setHttpOnly(true);

Java HttpCookie 類的setHttpOnly(Boolean httpOnly) 方法用于指示cookie 是否可以被認(rèn)為是HTTPOnly。如果設(shè)置為 true,則 cookie 不能被 JavaScript 等腳本引擎訪問。

句法

public void setHttpOnly(boolean httpOnly)  

范圍

上述方法只需要一個參數(shù):

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

到此這篇關(guān)于Java設(shè)置httponly cookie的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Java設(shè)置httponly cookie內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

  • 關(guān)于Java的二叉樹、紅黑樹、B+樹詳解

    關(guān)于Java的二叉樹、紅黑樹、B+樹詳解

    這篇文章主要介紹了關(guān)于Java的二叉樹、紅黑樹、B+樹詳解,能同時具備數(shù)組查找快的優(yōu)點(diǎn)以及鏈表插入和刪除快的優(yōu)點(diǎn)的數(shù)據(jù)結(jié)構(gòu)就是樹,需要的朋友可以參考下
    2023-05-05
  • SWT(JFace) 體驗(yàn)之FontRegistry

    SWT(JFace) 體驗(yàn)之FontRegistry

    測試代碼如下:
    2009-06-06
  • Java的jmap命令的具體使用

    Java的jmap命令的具體使用

    jmap是JDK提供的一個可以生成Java虛擬機(jī)的堆轉(zhuǎn)儲快照dump文件的命令行工具,本文主要介紹了Java的jmap命令的具體使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Java中ArrayList和LinkedList的區(qū)別

    Java中ArrayList和LinkedList的區(qū)別

    ArrayList和LinkedList在這個方法上存在一定的性能差異,本文就介紹了Java中ArrayList和LinkedList的區(qū)別,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • SpringBoot整合JavaMail通過阿里云企業(yè)郵箱發(fā)送郵件的實(shí)現(xiàn)

    SpringBoot整合JavaMail通過阿里云企業(yè)郵箱發(fā)送郵件的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot整合JavaMail通過阿里云企業(yè)郵箱發(fā)送郵件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • 走進(jìn)JDK之不可變類String

    走進(jìn)JDK之不可變類String

    這篇文章主要給大家介紹了JDK之不可變類String的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用JDK具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • SWT(JFace) 打印功能

    SWT(JFace) 打印功能

    SWT(JFace)體驗(yàn)之打印功能
    2009-06-06
  • drools的簡單入門案例場景分析

    drools的簡單入門案例場景分析

    drools是一款由JBoss組織提供的基于Java語言開發(fā)的開源規(guī)則引擎,可以將復(fù)雜且多變的業(yè)務(wù)規(guī)則從硬編碼中解放出來,這篇文章主要介紹了drools的簡單入門案例,需要的朋友可以參考下
    2022-05-05
  • Hibernate基于ThreadLocal管理Session過程解析

    Hibernate基于ThreadLocal管理Session過程解析

    這篇文章主要介紹了Hibernate基于ThreadLocal管理Session過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • Eclipse新建項(xiàng)目不可選擇Java Project問題解決方案

    Eclipse新建項(xiàng)目不可選擇Java Project問題解決方案

    這篇文章主要介紹了Eclipse新建項(xiàng)目不可選擇Java Project問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07

最新評論