您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java如何設(shè)置httponly cookie”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“Java如何設(shè)置httponly cookie”文章能幫助大家解決問題。
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)
上述方法只需要一個(gè)參數(shù):
httpOnly - 如果 cookie 僅是 HTTP,則表示 true,這意味著它作為 HTTP 請(qǐng)求的一部分可見。
不適用
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
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
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”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。