首頁 > Java > java教程 > 主體

如何在Java中設定HttpOnly Cookie?

PHPz
發布: 2023-04-22 18:37:08
轉載
1694 人瀏覽過

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?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!