最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

在支持httponly cookie的瀏覽器(IE6+、FF3.0+)中,如果cookie中設(shè)置了“httponly”屬性,則JavaScript腳本將無(wú)法讀取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 類(lèi)的setHttpOnly(Boolean httpOnly) 方法用于指示cookie 是否可以被認(rèn)為是HTTPOnly。如果設(shè)置為 true,則 cookie 不能被 JavaScript 等腳本引擎訪(fǎng)問(wèn)。

句法

public void setHttpOnly(boolean httpOnly)  

范圍

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

httpOnly - 如果 cookie 僅是 HTTP,則表示 true,這意味著它作為 HTTP 請(qǐng)求的一部分可見(jiàn)。

返回

不適用

示例 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

  • JAVA IO的3種類(lèi)型區(qū)別解析

    JAVA IO的3種類(lèi)型區(qū)別解析

    這篇文章主要介紹了JAVA IO的3種類(lèi)型解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • SpringBoot中的CompletableFuture類(lèi)詳解

    SpringBoot中的CompletableFuture類(lèi)詳解

    這篇文章主要介紹了SpringBoot中的CompletableFuture類(lèi)詳解,在?Java8中,引入了CompletableFuture類(lèi),它提供了一種簡(jiǎn)單而強(qiáng)大的方式來(lái)執(zhí)行異步任務(wù),今天我們就來(lái)詳細(xì)解讀一下這個(gè)類(lèi),需要的朋友可以參考下
    2023-07-07
  • Java concurrency線(xiàn)程池之線(xiàn)程池原理(二)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java concurrency線(xiàn)程池之線(xiàn)程池原理(二)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了Java concurrency線(xiàn)程池之線(xiàn)程池原理第二篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • SpringBoot啟動(dòng)參數(shù)的實(shí)現(xiàn)

    SpringBoot啟動(dòng)參數(shù)的實(shí)現(xiàn)

    SpringBoot通過(guò)jar文件方式啟動(dòng),配置可以通過(guò)啟動(dòng)參數(shù)進(jìn)行覆蓋,本文就來(lái)介紹一下SpringBoot啟動(dòng)參數(shù)的實(shí)現(xiàn),感興趣的可以了解一下
    2025-01-01
  • Spring中的@Autowired、@Qualifier和@Primary注解詳解

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

    這篇文章主要介紹了Spring中的@Autowired、@Qualifier和@Primary注解詳解,@Autowired?注解,可以對(duì)類(lèi)成員變量、方法和構(gòu)造函數(shù)進(jìn)行標(biāo)注,完成自動(dòng)裝配的工作,@Autowired?是默認(rèn)根據(jù)?byType?進(jìn)行自動(dòng)裝配的,需要的朋友可以參考下
    2023-11-11
  • 如何通過(guò)JVM角度談?wù)凧ava的clone操作

    如何通過(guò)JVM角度談?wù)凧ava的clone操作

    java中僅有的創(chuàng)建對(duì)象的兩種方式:①.使用new操作符創(chuàng)建對(duì)象;②.使用clone方法復(fù)制對(duì)象。下面這篇文章主要通過(guò)JVM角度給大家詳細(xì)談?wù)凧ava的clone操作的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-06-06
  • Springboot中PropertySource的結(jié)構(gòu)與加載過(guò)程逐步分析講解

    Springboot中PropertySource的結(jié)構(gòu)與加載過(guò)程逐步分析講解

    本文重點(diǎn)講解一下Spring中@PropertySource注解的使用,PropertySource主要是對(duì)屬性源的抽象,包含屬性源名稱(chēng)name和屬性源內(nèi)容對(duì)象source。其方法主要是對(duì)這兩個(gè)字段進(jìn)行操作
    2023-01-01
  • java最新版本連接mysql失敗的解決過(guò)程

    java最新版本連接mysql失敗的解決過(guò)程

    這篇文章主要給大家介紹了關(guān)于java最新版本連接mysql失敗的解決過(guò)程,文中通過(guò)圖文以及示例代碼將解決的過(guò)程介紹的非常詳細(xì),對(duì)遇到這個(gè)問(wèn)題的同學(xué)具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-09-09
  • 如何在Spring?Boot中使用OAuth2認(rèn)證和授權(quán)

    如何在Spring?Boot中使用OAuth2認(rèn)證和授權(quán)

    這篇文章主要介紹了如何在Spring?Boot中使用OAuth2認(rèn)證和授權(quán)的相關(guān)資料,OAuth2.0是一種開(kāi)放的授權(quán)協(xié)議,它允許用戶(hù)授權(quán)第三方應(yīng)用訪(fǎng)問(wèn)其賬戶(hù)(或資源),而無(wú)需共享其用戶(hù)賬戶(hù)憑據(jù),需要的朋友可以參考下
    2023-12-12
  • eclipse+myeclipse 環(huán)境配置方法

    eclipse+myeclipse 環(huán)境配置方法

    eclipse+myeclipse配置環(huán)境
    2009-07-07

最新評(píng)論

宜宾县| 苗栗县| 临猗县| 永川市| 根河市| 博湖县| 临猗县| 方城县| 呈贡县| 桓台县| 库尔勒市| 淳化县| 五台县| 昌邑市| 杂多县| 孙吴县| 通道| 汉阴县| 梓潼县| 奇台县| 二连浩特市| 池州市| 漾濞| 衡水市| 青龙| 呼伦贝尔市| 青浦区| 西吉县| 安康市| 蕉岭县| 馆陶县| 辽阳县| 通山县| 乌兰察布市| 南丰县| 鄢陵县| 泰来县| 阿拉善左旗| 镇巴县| 邛崃市| 都兰县|