Node cookie-session的驗證機制

什麼是HTTP & HTTP如何運作

HTTP遵循客戶端(client)-伺服器端(server)的模式
client =>(發送請求,request) => server(根據請求內容) =>(發送回應,response)=>client

圖片來源https://bytesofgigabytes.com/networking/how-http-request-and-response-works/

圖片來源:MDN HTTP Messages

安裝cookie-session

將客戶端的會話資料儲存在 cookie 中
透過npm安裝
npm cookie-session

1
2
nvm use 16.14.0
npm install cookie-session --save

在Node express使用

1
2
3
4
5
6
7
8
9
10
11
12
var cookieSession = require('cookie-session')
var express = require('express')

var app = express()

app.use(cookieSession({
name: 'session',
keys: [/* secret keys */],

// Cookie Options
maxAge: 24 * 60 * 60 * 1000 // 24 hours
}))

cookieSession(options)

使用提供的選項建立新的 cookie 會話中間件。 該中間件會將屬性 session 附加到 req,它提供了一個表示已載入會話的物件。 如果請求中未提供有效會話,則該會話是新會話,或是從請求載入的會話。

如果 req.session 的內容被更改,中間件將自動向回應添加 Set-Cookie 標頭。 請注意,除非會話中有內容,否則響應中不會出現 Set-Cookie 標頭(因此不會為特定用戶創建會​​話),因此一旦您有要存儲的標識信息,請務必向 req.session 添加一些內容會議。

Options

Cookie 會話接受選項物件中的這些屬性。
name:要設定的 cookie 的名稱,預設為 session。
keys用於簽署和驗證 cookie 值的金鑰列表,或配置的 Keygrip 實例。 設定 cookie 始終使用 key[0] 進行簽名,而其他金鑰對於驗證有效,從而允許金鑰輪換。 如果提供了 Keygrip 實例,則可以使用它來變更簽章參數,例如簽章演算法。
secret如果未提供鍵,則將用作單一鍵的字串。
Cookie Options
其他選項傳遞給 cookies.get() 和 cookies.set() ,可讓您控制安全性、網域、路徑和簽名等設定。

這些選項還可以包含以下任何內容(有關完整列表,請參閱 cookies 模組文件:

  • maxAge: a number representing the milliseconds from Date.now() for expiry
    最大年齡::代表 Date.now() 到期時間的毫秒數的數字
  • expires: a Date object indicating the cookie's expiration date (expires at the end of session by default).
    過期時間:指示 cookie 過期日期的 Date 物件(預設在會話結束時過期)。
  • path: a string indicating the path of the cookie (/ by default).
    指示 cookie 路徑的字串(預設為 /)。
  • domain: a string indicating the domain of the cookie (no default).
    指示 cookie domain的字串(無預設值)。
  • sameSite: a boolean or string indicating whether the cookie is a "same site" cookie (false by default). This can be set to 'strict', 'lax', 'none', or true (which maps to 'strict').
    一個布林值或字串,指示 cookie 是否是「同一網站」cookie(預設為 false)。 可以將其設定為“strict”、“lax”、“none”或 true(映射到“strict”)。
  • secure: a boolean indicating whether the cookie is only to be sent over HTTPS (false by default for HTTP, true by default for HTTPS). If this is set to true and Node.js is not directly over a TLS connection, be sure to read how to setup Express behind proxies or the cookie may not ever set correctly.
    安全的:一個布林值,指示 cookie 是否僅透過 HTTPS 發送(HTTP 預設為 false,HTTPS 預設為 true)。 如果將此設為 true 並且 Node.js 不是直接透過 TLS 連接,請務必閱讀如何在代理程式後面設定 Express,否則 cookie 可能無法正確設定。
  • httpOnly: a boolean indicating whether the cookie is only to be sent over HTTP(S), and not made available to client JavaScript (true by default).
    僅http:一個布林值,指示 cookie 是否僅透過 HTTP(S) 發送,而不可供客戶端 JavaScript 使用(預設為 true)。
  • signed: a boolean indicating whether the cookie is to be signed (true by default).
    簽署:一個布林值,指示 cookie 是否要簽署(預設為 true)。
  • overwrite: a boolean indicating whether to overwrite previously set cookies of the same name (true by default).
    覆蓋:一個布林值,指示是否覆蓋先前設定的同名 cookie(預設為 true)。

req.session

.isChanged

.isNew

.isPopulated

req.sessionOptions

Destroying a session

Saving a session

參考資料