Cookie 选项
2024年11月21日大约 3 分钟
Cookie 选项
描述 Eyeofcloud Edge Agent 中 Cookie 的默认设置以及如何覆盖它们。
👍
试用版
Eyeofcloud Edge Agent 目前处于测试阶段。在 Apply 上云眼 beta 版注册页面或联系您的客户成功经理。
cookieOptions
模块指定了在 Eyeofcloud Edge Agent 中设置 Cookie 的默认选项。此模块提供标准 Cookie 配置,确保不同环境之间的一致性和安全性。
创建 Cookie 时,Eyeofcloud Edge Agent 使用默认 Cookie 选项。但是,您可以通过在相关函数和方法调用中传递选项来覆盖这些默认值,以根据您的要求自定义 Cookie 行为。
默认 Cookie 选项
cookieDefaultOptions
对象包含 Eyeofcloud Edge Agent 使用的 Cookie 的默认设置。以下是每个选项的详细说明:
JavaScript (英语)
/**
* @module cookieOptions
*
* The CookieOptions specifies the default options for the cookies. *
*/
const cookieDefaultOptions = {
path: '/', // Default path for the cookie.
expires: new Date(Date.now() + 86400e3 * 365), // Sets expiration date to 365 days from now.
maxAge: 86400 * 365, // Maximum age of the cookie in seconds (365 days).
domain: '.expedge.com', // Domain where the cookie is valid.
secure: true, // Indicates if the cookie should be sent over secure protocol only.
httpOnly: true, // Indicates that the cookie is accessible only through the HTTP protocol.
sameSite: 'none', // Cross-site request setting for the cookie.
// Options are:
// - "Strict": The cookie will only be sent along with "same-site" requests.
// - "Lax": The cookie is not sent on cross-site requests except when navigating to the target site.
// - "None": The cookie will be sent on both same-site and cross-site requests. Requires `Secure` to be true.
};
选项说明
path
- 类型–字符串
- 默认 –
'/'
- 描述 – 指定浏览器发送 Cookie 标头的请求 URL 中的 URL 路径。将此项设置为可使 Cookie 对整个域可用。
'/'
expires
- 类型–日期
- 默认 –
new Date(Date.now() + 86400e3 \* 365)
- 描述– 设置 cookie 的到期日期。此日期设置为自当前日期起的 365 天,这意味着 Cookie 将在一年后过期。
maxAge
- 类型–数
- 默认 –
86400 \* 365
- Description (描述) – 设置 Cookie 的最长使用时间(以秒为单位)。此值设置为 365 天 (每天 86400 秒) ,与属性匹配。
expires
domain
- 类型–字符串
- 默认 –
.expedge.com
- Description (描述) – 指定此 Cookie 有效的域。前导点 (.) 授权 Cookie 对
expedge.com
的子域有效。
secure
- 类型–布尔
- 默认 –
true
- 描述 – 指示是否应仅通过 HTTPS 等安全协议传输 Cookie。将此项设置为通过确保仅通过加密连接发送 Cookie 来增强 Cookie 的安全性。
true
httpOnly
- 类型–布尔
- 默认 –
true
- 描述 – 限制通过
Document.cookie
属性通过 JavaScript 访问 Cookie。这有助于降低客户端脚本攻击的风险,例如跨站点脚本 (XSS)。
sameSite
- 类型–字符串
- 违约–没有
- 描述 – 控制 Cookie 的跨站点请求行为。它可以采用以下值之一:
strict
– cookie 仅与同一站点请求一起发送。lax
– 除非导航到目标站点,否则不会在跨站点请求上发送 cookie。none
– cookie 在同一站点和跨站点请求上发送。设置none
后,secure
属性也必须为 true。
出口
cookieDefaultOptions
对象将导出为模块的默认导出,使其易于导入并在应用程序的其他部分中使用。
JavaScript
export default cookieDefaultOptions;
用法
要在应用程序中使用默认 cookie 选项,请导入cookieDefaultOptions
模块并在设置 cookie 时应用它:
JavaScript
export default cookieDefaultOptions;
function setCookie(name, value) {
document.cookie = `${name}=${value}; path=${
cookieDefaultOptions.path
}; expires=${cookieDefaultOptions.expires.toUTCString()}; max-age=${cookieDefaultOptions.maxAge}; domain=${
cookieDefaultOptions.domain
}; secure=${cookieDefaultOptions.secure}; httpOnly=${cookieDefaultOptions.httpOnly}; sameSite=${
cookieDefaultOptions.sameSite
}`;
}
这可确保应用程序内的 Cookie 符合预定义的安全和行为标准。