> For the complete documentation index, see [llms.txt](https://gitbook.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.com/docs/documentation/zh/fa-bu/adaptive-content/enabling-adaptive-content/cookies.md).

# Cookie

通过公共或签名 Cookie 将访客数据传入你的文档

{% hint style="info" %}
前往我们的指南，找到一个 [完整操作指南](/docs/guides/docs-personalization-and-authentication/setting-up-adaptive-content.md) 了解如何使用 cookie 设置自适应内容。
{% endhint %}

{% hint style="warning" %}
在功能标志中使用自适应内容需要向你的应用程序添加代码。

此方法仅在你的网站托管于以下域名下时有效： [自定义域名](/docs/documentation/zh/fa-bu/custom-domain.md).
{% endhint %}

你可以通过访客浏览器中的 cookie 将访客数据传递给你的文档。以下是不同方法的概览。

<table data-full-width="false"><thead><tr><th width="335.125">方法</th><th width="266.6015625">用例</th><th width="206.58984375">设置难易程度</th><th width="202">安全性</th><th>格式</th></tr></thead><tbody><tr><td>已签名 cookie <code>gitbook-visitor-token</code></td><td>API 测试凭据、客户识别</td><td>需要签名和自定义域名</td><td><span data-gb-custom-inline data-tag="emoji" data-code="2705">✅</span> 属性只能由后端定义</td><td>JWT</td></tr><tr><td>公共 cookie <code>gitbook-visitor-public</code></td><td>功能标志、角色</td><td>易于设置</td><td><span data-gb-custom-inline data-tag="emoji" data-code="274c">❌</span> 访客可以覆盖这些属性</td><td>JSON</td></tr></tbody></table>

### 公共 cookie

要将数据从公共 cookie 传递给 GitBook，你需要通过设置一个公共 `gitbook-visitor-public` cookie。

下面是一个简单的 JavaScript 示例：

```javascript
import Cookies from 'js-cookie';

const cookieData = {
  isLoggedIn: true,
  isBetaUser: false,
};

Cookies.set('gitbook-visitor-public', JSON.stringify(cookieData), {
  secure: true,
  domain: '*.acme.org',
})
```

{% hint style="warning" %}
通过公共 cookie 传递的数据必须在你的访客架构中通过一个 [未签名的](/docs/documentation/zh/fa-bu/adaptive-content/enabling-adaptive-content.md#set-an-unsigned-claim) 对象。
{% endhint %}

### 已签名 cookie

要更安全地将数据传递给 GitBook，你需要将数据作为 [JSON Web Token](https://jwt.io/introduction) 从你的应用程序中通过一个名为 `gitbook-visitor-token` 并绑定到你的域名的 cookie 中发送。

要进行设置，你需要调整应用程序的登录流程，包含以下步骤：

{% stepper %}
{% step %}
**当用户登录你的应用程序时生成一个 JWT**

每当用户登录你的产品时，生成一个包含已认证用户信息中选定属性的 JWT。
{% endstep %}

{% step %}
**使用站点的访客签名密钥对 JWT 进行签名**

然后，确保使用站点的 **访客签名密钥**进行签名；启用自适应内容后，你可以在站点的受众设置中找到该密钥。
{% endstep %}

{% step %}
**将 JWT 存储在通配符会话 cookie 中**

最后，你需要将包含用户信息的已签名 JWT 存储到一个通配符会话 cookie 中 **位于你的产品域名下**.

例如，如果你的应用程序托管在 `app.acme.org` 域名下，则需要在 `.acme.org` 通配符域名下创建该 cookie。
{% endstep %}
{% endstepper %}

下面是一个简单的 TypeScript 示例：

```typescript
import * as jose from 'jose';

import { Request, Response } from 'express';

import { getUserInfo } from '../services/user-info-service';
import { getFeatureFlags } from '../services/feature-flags-service';

const GITBOOK_VISITOR_SIGNING_KEY = process.env.GITBOOK_VISITOR_SIGNING_KEY;
const GITBOOK_VISITOR_COOKIE_NAME = 'gitbook-visitor-token';


export async function handleAppLoginRequest(req: Request, res: Response) {
   // 你的业务逻辑，用于处理登录请求
   // 例如，检查凭据并验证用户
   //
   // 例如：
   // const loggedInUser = await authenticateUser(req.body.username, req.body.password);

   // 在验证用户身份后，获取你希望
   // 从数据库或用户服务传递给 GitBook 的用户信息。
   const userInfo = await getUserInfo(loggedInUser.id);
      
   // 使用用户信息构建 JWT 有效载荷
   const gitbookVisitorClaims = {
       firstName: userInfo.firstName,
       lastName: userInfo.lastName,
       isBetaUser: userInfo.isBetaUser
       products: userInfo.products.map((product) => product.name),
       featureFlags: await getFeatureFlags({userId: loggedInUser.id})
   }
   
   // 使用这些声明生成一个已签名的 JWT
   const gitbookVisitorJWT = await new jose.SignJWT(gitbookVisitorClaims)
     .setProtectedHeader({ alg: 'HS256' })
     .setIssuedAt()
     .setExpirationTime('2h') // 任意设置 2 小时过期
     .sign(GITBOOK_VISITOR_SIGNING_KEY);
     
  // 在你的
  // 登录处理器响应中包含一个带有编码后 JWT 的 `gitbook-visitor-token` cookie
  res.cookie(GITBOOK_VISITOR_COOKIE_NAME, gitbookVisitorJWT, {
     httpOnly: true,
     secure: process.env.NODE_ENV === 'production',
     maxAge: 2 * 60 * 60 * 1000, // 任意设置 2 小时过期
     domain: '.acme.org' //
  });
  
  // 登录处理器的其余逻辑，包括将用户重定向到你的应用程序
  res.redirect('/'); // 示例重定向
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://gitbook.com/docs/documentation/zh/fa-bu/adaptive-content/enabling-adaptive-content/cookies.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
