. Js 操作 Cookie.
一般來說,只有服務器操作Cookie 才能保證一些必要的安全。但有時候,可能需要前端來增刪改查 Cookie, 這個時候咱們的主角出現了——HttpOnly(๑•̀ㅂ•́) ✧.
HttpOnly: HttpOnly is an additional flag included in a Set-Cookie HTTP response header. Using the HttpOnly flag when generating a cookie helps mitigate the risk of client side script accessing the protected cookie (if the browser supports it).
來着谷歌翻譯:
HttpOnly是包含在Set-Cookie HTTP響應頭文件中的附加標志。生成cookie時使用HttpOnly標志有助於降低客戶端腳本訪問受保護cookie的風險(如果瀏覽器支持)。
這個意思就是說,如果某一個Cookie 選項被設置成 HttpOnly = true 的話,那此Cookie 只能通過服務器端修改,Js 是操作不了的,對於 document.cookie 來說是透明的。話不多說,直接上圖:
console里面輸入
document.cookie
"wd=1922x912; c_user=1201711570"
可以看到只能看到非httpOnly的