pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

我必須將令牌存儲在 cookie 或本地存儲或會話中嗎

Do I have to store tokens in cookies or localstorage or session?(我必須將令牌存儲在 cookie 或本地存儲或會話中嗎?)
本文介紹了我必須將令牌存儲在 cookie 或本地存儲或會話中嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在使用 React SPA、Express、Express-session、Passport 和 JWT.我對一些用于存儲令牌的不同客戶端存儲選項感到困惑:Cookie、Session 和 JWT/Passport.

I am using React SPA, Express, Express-session, Passport, and JWT. I'm confused about some of the different client-side storage options to store tokens: Cookies, Session, and JWT / Passport.

令牌是否必須存儲在 cookie 中,即使我可以將它們存儲在 req.sessionID 中?

Do tokens have to be stored in cookies, even if I can store them in req.sessionID?

許多網站使用 cookie 來存儲購物車令牌.到目前為止,我已經根據會話 ID 存儲了購物車數據,而沒有添加任何 cookie.

Many websites use cookies to store shopping cart tokens. So far I have stored shopping cart data based on the session ID without adding any cookies.

所以當用戶訪問我的網站時,我會將其與他們的req.sessionID 然后檢索數據庫中的數據,如購物車和用戶會話.

So when users visit my website, I will match it with their req.sessionID and then retrieve the data in the database like shopping carts and user session.

我需要存儲 cookie 嗎?我可以通過 req.sessionID 訪問它以獲取所需的數據.

Do I need to store cookies? I can access it via req.sessionID to get the data needed.

還有第二個

我已使用 passport-google-oauth20 進行身份驗證.成功登錄后,數據將保存到會話中.并將其發送給客戶端,我必須通過 URL 查詢 ?token='sdsaxas' 發送它.

I have made authentication using a passport-google-oauth20.After I successfully login, the data is saved into the session. and to send it to the client I have to send it via the URL query ?token='sdsaxas'.

在這種情況下,我有很多不同的意見.有人保存了它到本地存儲中,有人通過使用 JWT 將其轉換為令牌將其保存到 cookie 中.

in this case I get a lot of difference of opinion. someone saved it into local storage and someone saved it into cookies by converting it to a token using JWT.

 jwt.sign(
        payload,
        keys.jwt.secretOrPrivateKey, 
        {
            expiresIn:keys.jwt.expiresIn // < i dont know what is this expired for cookies or localstorage ?
        }, (err, token) => {

            res.redirect(keys.origin.url + "?token=" + token);
        });

我真的可以使用 sessionID(不帶 cookie 或本地存儲)存儲與會話相關的所有內容嗎?

因為我使用 React SPA,所以只需要進行一次或每次頁面刷新并檢索數據然后保存到 redux 中.

Only by doing fetch once or every page refresh and retrieving the data and then saved into redux because I use React SPA.

推薦答案

這個答案是基于無狀態的方法,因此它沒有談論傳統的會話管理

你問了兩個完全不同的問題:

You have asked two altogether different questions:

  1. 購物車 - 與業務功能更相關
  2. OAuth 2 &JWT - 與安全和身份驗證有關

作為電子商務網站的用戶,我希望在上下班途中從移動設備添加到購物車中的任何商品在我從 PC 登錄到網站后,都應該可以在購物車中找到到達家.因此,購物車數據應保存在后端數據庫中并鏈接到我的用戶帳戶.

As an user of an ecommerce website, I'd expect that any item I add to my shopping cart from my mobile device while commuting to my workplace, should be available in the cart when I login to the website from my PC after reaching home. Therefore, the cart data should be saved in the back-end DB and linked to my user account.

在使用 OAuth 2.0 進行身份驗證時,JWT 訪問令牌和/或刷新令牌需要存儲在客戶端設備中的某個位置,這樣一旦用戶通過提供登錄憑據進行身份驗證,他就不需要提供他的憑據再次瀏覽該網站.在這種情況下,瀏覽器本地存儲、會話存儲和 cookie 都是有效的選項.但是,請注意這里的 cookie 沒有鏈接到服務器端的任何會話.換句話說,cookie 不存儲任何會話 ID.cookie 僅用作訪問令牌的存儲,該令牌隨每個 http 請求傳遞給服務器,然后服務器使用數字簽名驗證令牌,以確保它沒有被篡改并且沒有過期.

When it comes to authentication using OAuth 2.0, the JWT access token and / or refresh token need to be stored somewhere in the client device, so that once the user authenticates himself by providing login credentials, he doesn't need to provide his credentials again to navigate through the website. In this context, the browser local storage, session storage and cookies are all valid options. However, note that here the cookie is not linked to any session on the server side. In other words, the cookie doesn't store any session id. The cookie is merely used as a storage for access token which is passed to the server with every http request and the server then validates the token using the digital signature to ensure that it is not tampered and it is not expired.

雖然訪問和/或刷新令牌的所有三個存儲選項都很流行,但如果以正確的方式使用,cookie 似乎是最安全的選項.

Although all three storage options for access and / or refresh tokens are popular, cookie seems to be the most secured option when used in the correct way.

為了更好地理解這一點,我建議您閱讀 this 和 this 以及 OAuth 2.0 規范.

To understand this better, I recommend you read this and this along with the OAuth 2.0 specification.

我之前說過,cookie 似乎是最安全的選項.我想在這里進一步澄清這一點.

I said earlier that cookie seems to be the most secured options. I'd like to further clarify the point here.

我認為瀏覽器 localStoragesessionStorage 沒有為存儲身份驗證令牌提供足夠的安全性的原因如下:

The reason I think browser localStorage and sessionStorage do not provide enough security for storing auth tokens are as follows:

  1. 如果發生 XSS,惡意腳本可以輕松地從那里讀取令牌并將其發送到遠程服務器.從那里開始,遠程服務器或攻擊者在冒充受害者用戶時不會有任何問題.

  1. If XSS occurs, the malicious script can easily read the tokens from there and send them to a remote server. There on-wards the remote server or attacker would have no problem in impersonating the victim user.

localStoragesessionStorage 不跨子域共享.因此,如果我們在不同的子域上運行兩個 SPA,我們將無法獲得 SSO 功能,因為一個應用程序存儲的令牌將無法用于組織內的另一個應用程序.有一些使用 iframe 的解決方案,但這些看起來更像是變通方法,而不是一個好的解決方案.而當響應頭 X-Frame-Options 用于避免使用 iframe 的點擊劫持攻擊時,任何使用 iframe 的解決方案都是沒有問題的.

localStorage and sessionStorage are not shared across sub-domains. So, if we have two SPA running on different sub-domains, we won't get the SSO functionality because the token stored by one app won't be available to the other app within the organization. There are some solutions using iframe, but those look more like workarounds rather than a good solution. And when the response header X-Frame-Options is used to avoid clickjacking attacks with iframe, any solution with iframe is out of question.

但是,這些風險可以通過使用指紋來降低(如 OWASP JWT Cheat Sheet) 這又需要一個 cookie.

These risks can, however, be mitigated by using a fingerprint (as mentioned in OWASP JWT Cheat Sheet) which again in turn requires a cookie.

指紋的想法是,生成一個加密強的隨機字節串.然后將原始字符串的 Base64 字符串存儲在 HttpOnlySecureSameSite cookie 中,名稱前綴為 __Secure-.應根據業務需求使用正確的域和路徑屬性值.字符串的 SHA256 哈希也將在 JWT 的聲明中傳遞.因此,即使 XSS 攻擊將 JWT 訪問令牌發送到攻擊者控制的遠程服務器,它也無法在 cookie 中發送原始字符串,因此服務器可以根據 cookie 的缺失拒絕請求.XSS 腳本無法讀取 HttpOnly 的 cookie.

The idea of fingerprint is, generate a cryptographically strong random string of bytes. The Base64 string of the raw string will then be stored in a HttpOnly, Secure, SameSite cookie with name prefix __Secure-. Proper values for Domain and Path attributes should be used as per business requirement. A SHA256 hash of the string will also be passed in a claim of JWT. Thus even if an XSS attack sends the JWT access token to an attacker controlled remote server, it cannot send the original string in cookie and as a result the server can reject the request based on the absence of the cookie. The cookie being HttpOnly cannot be read by XSS scripts.

因此,即使我們使用 localStoragesessionStorage,我們也必須使用 cookie 來確保其安全.最重要的是,我們添加了上面提到的子域限制.

Therefore, even when we use localStorage and sessionStorage, we have to use a cookie to make it secured. On top of that, we add the sub-domain restriction as mentioned above.

現在,使用 cookie 存儲 JWT 的唯一問題是 CSRF 攻擊.由于我們使用 SameSite cookie,CSRF 得到緩解,因為跨站點請求(AJAX 或僅通過超鏈接)是不可能的.如果該站點用于任何舊瀏覽器或其他一些不那么流行的不支持 SameSite cookie 的瀏覽器,我們仍然可以通過另外使用具有加密強隨機值的 CSRF cookie 來緩解 CSRF,這樣每個AJAX 請求讀取 cookie 值并將 cookie 值添加到自定義 HTTP 標頭中(GET 和 HEAD 請求除外,它們不應該進行任何狀態修改).由于 CSRF 由于同源策略而無法讀取任何內容,并且它基于利用 POST、PUT 和 DELETE 等不安全的 HTTP 方法,因此此 CSRF cookie 將減輕 CSRF 風險.所有現代 SPA 框架都使用這種使用 CSRF cookie 的方法.這里提到了 Angular 方法.

Now, the only concern about using a cookie to store JWT is, CSRF attack. Since we use SameSite cookie, CSRF is mitigated because cross-site requests (AJAX or just through hyperlinks) are not possible. If the site is used in any old browser or some other not so popular browsers that do not support SameSite cookie, we can still mitigate CSRF by additionally using a CSRF cookie with a cryptographically strong random value such that every AJAX request reads the cookie value and add the cookie value in a custom HTTP header (except GET and HEAD requests which are not supposed to do any state modifications). Since CSRF cannot read anything due to same origin policy and it is based on exploiting the unsafe HTTP methods like POST, PUT and DELETE, this CSRF cookie will mitigate the CSRF risk. This approach of using CSRF cookie is used by all modern SPA frameworks. The Angular approach is mentioned here.

另外,由于 cookie 是 httpOnlySecured,XSS 腳本無法讀取它.因此 XSS 也得到了緩解.

Also, since the cookie is httpOnly and Secured, XSS script cannot read it. Thus XSS is also mitigated.

值得一提的是,可以通過使用適當的 content-security-policy 響應標頭進一步緩解 XSS 和腳本注入.

It may be also worth mentioning that XSS and script injection can be further mitigated by using appropriate content-security-policy response header.

  1. 狀態變量(Auth0 使用它)- 客戶端將生成并隨每個請求傳遞一個加密的強隨機隨機數,服務器將連同其響應一起回顯該隨機數,從而允許客戶端驗證隨機數.Auth0 文檔中對此進行了說明.
  2. 始終檢查referer 標頭并僅在referer 是受信任的域時接受請求.如果沒有引用標頭或未列入白名單的域,則只需拒絕請求.使用 SSL/TLS 時,通常會出現引用.登陸頁面(主要是信息性的,不包含登錄表單或任何安全內容)可能有點放松,并允許缺少引用標頭的請求.
  3. 應在服務器中阻止 TRACE HTTP 方法,因為這可用于讀取 httpOnly cookie.
  4. 另外,設置標題 Strict-Transport-Security: max-age=;includeSubDomains 只允許安全連接,以防止任何中間人覆蓋子域中的 CSRF cookie.
  1. State Variable (Auth0 uses it) - The client will generate and pass with every request a cryptographically strong random nonce which the server will echo back along with its response allowing the client to validate the nonce. It's explained in Auth0 doc.
  2. Always check the referer header and accept requests only when referer is a trusted domain. If referer header is absent or a non-whitelisted domain, simply reject the request. When using SSL/TLS referrer is usually present. Landing pages (that is mostly informational and not containing login form or any secured content) may be little relaxed ?and allow requests with missing referer header.
  3. TRACE HTTP method should be blocked in the server as this can be used to read the httpOnly cookie.
  4. Also, set the header Strict-Transport-Security: max-age=; includeSubDomains? to allow only secured connections to prevent any man-in-the-middle overwrite the CSRF cookies from a sub-domain.

這篇關于我必須將令牌存儲在 cookie 或本地存儲或會話中嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Is Math.random() cryptographically secure?(Math.random() 在密碼學上是安全的嗎?)
Secure random numbers in javascript?(在javascript中保護隨機數?)
How to avoid multiple token refresh requests when making simultaneous API requests with an expired token(使用過期令牌發出同時 API 請求時如何避免多個令牌刷新請求)
JWT not decoding quot;JWT malformedquot; - Node Angular(JWT 未解碼“JWT malformed;- 節點角度)
How to invalidate a JWT token with no expiry time(如何使沒有到期時間的 JWT 令牌無效)
Authorization header in img src link(img src 鏈接中的授權標頭)
主站蜘蛛池模板: pbootcms网站模板|织梦模板|网站源码|jquery建站特效-html5模板网 | 富森高压水枪-柴油驱动-养殖场高压清洗机-山东龙腾环保科技有限公司 | 陕西鹏展科技有限公司 | 生物风-销售载体,基因,质粒,ATCC细胞,ATCC菌株等,欢迎购买-百风生物 | 荣事达手推洗地机_洗地机厂家_驾驶式扫地机_工业清洁设备 | 高博医疗集团上海阿特蒙医院 | SRRC认证|CCC认证|CTA申请_IMEI|MAC地址注册-英利检测 | 西安展台设计搭建_西安活动策划公司_西安会议会场布置_西安展厅设计西安旭阳展览展示 | 隐形纱窗|防护纱窗|金刚网防盗纱窗|韦柏纱窗|上海青木装潢制品有限公司|纱窗国标起草单位 | SDI车窗夹力测试仪-KEMKRAFT方向盘测试仪-上海爱泽工业设备有限公司 | 烟雾净化器-滤筒除尘器-防爆除尘器-除尘器厂家-东莞执信环保科技有限公司 | 新型游乐设备,360大摆锤游乐设备「诚信厂家」-山东方鑫游乐设备 新能源汽车电池软连接,铜铝复合膜柔性连接,电力母排-容发智能科技(无锡)有限公司 | 等离子表面处理机-等离子表面活化机-真空等离子清洗机-深圳市东信高科自动化设备有限公司 | C形臂_动态平板DR_动态平板胃肠机生产厂家制造商-普爱医疗 | 福兰德PVC地板|PVC塑胶地板|PVC运动地板|PVC商用地板-中国弹性地板系统专业解决方案领先供应商! 福建成考网-福建成人高考网 | 混合气体腐蚀试验箱_盐雾/硫化氢/气体腐蚀试验箱厂家-北京中科博达 | MES系统工业智能终端_生产管理看板/安灯/ESOP/静电监控_讯鹏科技 | 山东艾德实业有限公司| 低浓度恒温恒湿称量系统,强光光照培养箱-上海三腾仪器有限公司 | 我车网|我关心的汽车资讯_汽车图片_汽车生活! | 复盛空压机配件-空气压缩机-复盛空压机(华北)总代理 | 土壤养分检测仪|土壤水分|土壤紧实度测定仪|土壤墒情监测系统-土壤仪器网 | 贝朗斯动力商城(BRCPOWER.COM) - 买叉车蓄电池上贝朗斯商城,价格更超值,品质有保障! | 颗粒机,颗粒机组,木屑颗粒机-济南劲能机械有限公司 | 英国雷迪地下管线探测仪-雷迪RD8100管线仪-多功能数字听漏仪-北京迪瑞进创科技有限公司 | 定硫仪,量热仪,工业分析仪,马弗炉,煤炭化验设备厂家,煤质化验仪器,焦炭化验设备鹤壁大德煤质工业分析仪,氟氯测定仪 | 无痕胶_可移胶_无痕双面胶带_可移无痕胶厂家-东莞凯峰 | 杭州|上海贴标机-百科| 阿里巴巴诚信通温州、台州、宁波、嘉兴授权渠道商-浙江联欣科技提供阿里会员办理 | 加盟店-品牌招商加盟-创业项目商机平台 | 小区健身器材_户外健身器材_室外健身器材_公园健身路径-沧州浩然体育器材有限公司 | 真空乳化机-灌装封尾机-首页-温州精灌 | 上海律师事务所_上海刑事律师免费咨询平台-煊宏律师事务所 | 合肥活动房_安徽活动板房_集成打包箱房厂家-安徽玉强钢结构集成房屋有限公司 | SRRC认证_电磁兼容_EMC测试整改_FCC认证_SDOC认证-深圳市环测威检测技术有限公司 | 六维力传感器_六分量力传感器_模腔压力传感器-南京数智微传感科技有限公司 | 气动隔膜泵厂家-温州永嘉定远泵阀有限公司 | 螺杆式冷水机-低温冷水机厂家-冷冻机-风冷式-水冷式冷水机-上海祝松机械有限公司 | 标准光源箱|对色灯箱|色差仪|光泽度仪|涂层测厚仪_HRC大品牌生产厂家 | 筛分机|振动筛分机|气流筛分机|筛分机厂家-新乡市大汉振动机械有限公司 | 气弹簧定制-气动杆-可控气弹簧-不锈钢阻尼器-工业气弹簧-可调节气弹簧厂家-常州巨腾气弹簧供应商 |