# 设置自定义后端

{% hint style="warning" %}
本指南将带你完成为文档设置受保护的登录界面。在阅读本指南之前，请确保你已经先完成了 [启用经过身份验证的访问](/docs/documentation/zh/zhan-dian-fang-wen/authenticated-access/enabling-authenticated-access.md).
{% endhint %}

本指南将带你完成使用你自己的 **自定义** 身份验证后端，为你的 GitBook 文档站点设置受保护的登录界面。

{% hint style="info" %}
如果你使用的是我们支持的身份验证提供商之一，或者你有一个 [OpenID Connect](https://auth0.com/docs/authenticate/protocols/openid-connect-protocol) （OIDC）兼容后端，请查看我们的集成指南，以获得更简化的设置：\
\
[Auth0](/docs/documentation/zh/zhan-dian-fang-wen/authenticated-access/setting-up-auth0.md) | [Azure AD](/docs/documentation/zh/zhan-dian-fang-wen/authenticated-access/setting-up-azure-ad.md) | [Okta](/docs/documentation/zh/zhan-dian-fang-wen/authenticated-access/setting-up-okta.md) | [AWS Cognito](/docs/documentation/zh/zhan-dian-fang-wen/authenticated-access/setting-up-aws-cognito.md) | [OIDC](/docs/documentation/zh/zhan-dian-fang-wen/authenticated-access/setting-up-oidc.md)
{% endhint %}

### 概览

要为你的 GitBook 站点设置自定义身份验证系统，请遵循以下关键步骤：

{% stepper %}
{% step %}
[**创建一个自定义后端来验证你的用户**](#id-1.-create-a-custom-backend-to-authenticate-your-users)

实现一个提示用户登录并对其进行身份验证的后端。
{% endstep %}

{% step %}
[**将 JWT 令牌签名后传递给 GitBook**](#id-2.-sign-and-pass-a-jwt-token-to-gitbook)

创建一个 JWT 令牌，并使用你站点的私钥对其签名。
{% endstep %}

{% step %}
[**配置备用 URL**](#id-3.-configure-a-fallback-url)

配置一个 URL，供未经过身份验证的访客访问你的站点时使用。
{% endstep %}

{% step %}
[**设置多租户身份验证访问（可选）**](#id-4.-set-up-multi-tenant-authenticated-access)

配置你的后端以处理跨多个 GitBook 站点的身份验证。
{% endstep %}

{% step %}
[**为自适应内容配置你的后端（可选）**](#id-5.-configure-your-backend-for-adaptive-content)

配置你的后端以配合 GitBook 中的自适应内容使用。
{% endstep %}
{% endstepper %}

### 1. 创建一个自定义后端来验证你的用户

为了在用户访问你的文档之前开始对其进行身份验证，你需要设置一个能够处理用户登录和身份验证的服务器。

你的后端应该：

* 提示用户使用你偏好的身份验证方式登录。
* 验证用户凭据并对其进行身份验证。
* 生成并签名一个 **JSON Web Token（JWT）** ，在身份验证成功后。
* 在 URL 中包含 JWT，将用户重定向到 GitBook。

### 2. 将 JWT 令牌签名后传递给 GitBook

一旦你的后端验证了用户，它必须 **生成一个 JWT** 并 **将其传递给 GitBook** 当 **重定向** 他们到你的站点时。令牌应使用 **私钥** 签名，该私钥可在你的站点受众设置中找到，位于 [启用经过身份验证的访问](/docs/documentation/zh/zhan-dian-fang-wen/authenticated-access/enabling-authenticated-access.md#enable-authenticated-access).

以下示例应展示你自定义后端中的登录请求处理程序可能是什么样子：

{% code title="index.ts" %}

```typescript
import { Request, Response } from 'express';
import * as jose from 'jose';

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_DOCS_URL = 'https://mycompany.gitbook.io/myspace';

export async function handleAppLoginRequest(req: Request, res: Response) {
    // 处理登录请求的业务逻辑
    // 例如，检查凭据并验证用户身份
    //
    // 例如：
    // const loggedInUser = await authenticateUser(req.body.username, req.body.password);
    
    // 生成已签名的 JWT
    const gitbookVisitorJWT = await new jose.SignJWT({})
        .setProtectedHeader({ alg: 'HS256' })
        .setIssuedAt()
        .setExpirationTime('2h') // 任意的 2 小时过期时间
        .sign(new TextEncoder().encode(GITBOOK_VISITOR_SIGNING_KEY));
    
    // 将用户重定向到 URL 中包含 JWT 令牌的 GitBook
    const redirectURL = `${GITBOOK_DOCS_URL}/?jwt_token=${gitbookVisitorJWT}`;
    res.redirect(redirectURL);
}
```

{% endcode %}

### 3. 配置备用 URL

当未经过身份验证的访客尝试访问你的受保护站点时，会使用备用 URL。然后 GitBook 会将他们重定向到这个 URL。

此 URL 应指向你自定义后端中的一个处理程序，在那里你可以提示他们登录、进行身份验证，然后将他们重定向回你的站点，并在 URL 中包含 JWT。

例如，如果你的登录界面位于 `https://example.com/login`，你应将此值作为备用 URL。

你可以在站点受众设置中的“Authenticated access”选项卡下配置此备用 URL。

<figure><img src="/files/1973886baa00c381d91acf27814e6b8f78dc0858" alt="A GitBook screenshot showing where to configure a fallback URL"><figcaption><p>配置备用 URL</p></figcaption></figure>

#### 使用 GitBook 的登录端点

如果你想在已发布的站点上放置登录链接，请链接到 `<publishedSiteURL>/~gitbook/auth/login`.

此端点会将访客重定向到为该站点配置的身份验证后端。它还会添加一个 `location` 查询参数，其值与他们开始访问时所在的页面相匹配。

这对于页眉链接和其他入口点很有用，你希望访客登录后返回到同一页面。

当重定向到备用 URL 时，GitBook 会在备用 URL 中包含一个 `location` 查询参数，你可以在处理程序中利用它将用户重定向回其原始位置：

```javascript
const gitbookVisitorJWT = await new jose.SignJWT({})
    .setProtectedHeader({ alg: 'HS256' })
    .setIssuedAt()
    .setExpirationTime('2h') // 任意的 2 小时过期时间
    .sign(new TextEncoder().encode(GITBOOK_VISITOR_SIGNING_KEY));
    
// 重定向到原始 GitBook 文档 URL，并将 JWT 作为 jwt_token 查询参数包含在内
// 如果提供了 location，用户将被重定向回其最初的目标页面
const redirectURL = `${GITBOOK_DOCS_URL}/${req.query.location || ''}?jwt_token=${gitbookVisitorJWT}`;
res.redirect(redirectURL);
```

{% hint style="warning" %}
因为 GitBook 依赖 `location` 搜索参数 - 你不能在备用 URL 中使用它。例如， `https://auth.gitbook.com/?location=something` 不是一个有效的备用 URL。
{% endhint %}

#### 使用 GitBook 的登出端点

如果你想在已发布的站点上放置登出链接，请链接到 `<publishedSiteURL>/~gitbook/auth/logout`.

此端点会将访客从其 GitBook 会话中登出。

### 4. 设置多租户身份验证访问（可选）

如果你使用 GitBook 作为向不同客户提供内容的平台，那么你可能需要设置多租户身份验证访问。你的身份验证后端需要负责处理跨多个不同站点的身份验证。通过对自定义身份验证后端代码做一些小调整，这在 GitBook 中是可行的。

#### 将所有租户添加到你的身份验证服务器

你的身份验证后端需要知道 JWT 签名密钥以及它预计要处理的所有 GitBook 站点的 URL。如果你的组织中有两个站点分别对应 Customer A 和 Customer B，你可以设想你的身份验证代码存储如下映射：

```typescript
const CUSTOMER_A = {
  jwtSigningKey: 'aaa-aaa-aaa-aaa',
  url: 'https://mycompany.gitbook.io/customer-a'
};

const CUSTOMER_B = {
  jwtSigningKey: 'bbb-bbb-bbb-bbb',
  url: 'https://mycompany.gitbook.io/customer-b'
};
```

#### 为你的身份验证服务器提供额外上下文

当 GitBook 无法验证用户的请求时，它会将用户重定向到备用 URL。此 URL 指向你的身份验证后端，后者负责对用户进行身份验证并将其重定向回所请求的内容。

为了支持多个租户，你的身份验证后端需要知道用户打算访问的是哪个 GitBook 站点。这个信息可以通过备用 URL 传递。

例如，你可以按如下方式为每个站点设置备用 URL：

<table><thead><tr><th width="150.75390625">GitBook 站点</th><th>备用 URL</th></tr></thead><tbody><tr><td>Customer A 站点</td><td><code>https://auth-backend.acme.org/login?site=customer-a</code></td></tr><tr><td>Customer B 站点</td><td><code>https://auth-backend.acme.org/login?site=customer-b</code></td></tr></tbody></table>

然后你的身份验证后端可以检查这些信息，并相应地处理重定向到正确的站点：

```javascript
const customerInfo = req.query.site === 'customer-a' ? CUSTOMER_A : CUSTOMER_B;
  
const gitbookVisitorJWT = await new jose.SignJWT({})
    .setProtectedHeader({ alg: 'HS256' })
    .setIssuedAt()
    .setExpirationTime('2h') // 任意的 2 小时过期时间
    .sign(new TextEncoder().encode(customerInfo.jwtSigningKey));
    
// 重定向到原始 GitBook 文档 URL，并将 JWT 作为 jwt_token 查询参数包含在内
// 如果提供了 location，用户将被重定向回其最初的目标页面
const redirectURL = `${customerInfo.url}/${req.query.location || ''}?jwt_token=${gitbookVisitorJWT}`;
res.redirect(redirectURL);
```

### 5. 为你的后端配置自适应内容（可选）

要在你的身份验证访问设置中利用自适应内容功能，你可以在自定义后端生成的 JWT 负载中包含额外的用户属性（claims），并在将用户重定向到站点时将其包含在 URL 中。

这些 claims 在 JWT 中包含后，会被 GitBook 用于 [自适应内容](/docs/documentation/zh/zhan-dian-fang-wen/adaptive-content/adapting-your-content.md) ，为你的站点访客动态调整内容。

综合起来，下面的代码示例演示了如何在 JWT 中包含这些 claims，之后 GitBook 可以使用它们为你的访客调整内容：

{% code title="index.ts" %}

```typescript
import { Request, Response } from 'express';
import * as jose from 'jose';

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_DOCS_URL = 'https://mycompany.gitbook.io/myspace';

export async function handleAppLoginRequest(req: Request, res: Response) {
    // 处理登录请求的业务逻辑
    // 例如，检查凭据并验证用户身份
    //
    // 例如：
    // const loggedInUser = await authenticateUser(req.body.username, req.body.password);
    
    // 为了本示例，假设有一个已登录用户对象
    const loggedInUser = { id: '12345' }; // 替换为实际的身份验证逻辑

    // 检索要传递给 GitBook 的用户信息
    const userInfo = await getUserInfo(loggedInUser.id);
    
    // 生成已签名的 JWT，并将用户属性作为 claims 包含进去
    const gitbookVisitorClaims = {
        firstName: userInfo.firstName,
        lastName: userInfo.lastName,
        isBetaUser: userInfo.isBetaUser,
        products: userInfo.products.map((product) => product.name),
        featureFlags: await getFeatureFlags({ userId: loggedInUser.id })
    };
    
    const gitbookVisitorJWT = await new jose.SignJWT(gitbookVisitorClaims)
        .setProtectedHeader({ alg: 'HS256' })
        .setIssuedAt()
        .setExpirationTime('2h') // 任意的 2 小时过期时间
        .sign(new TextEncoder().encode(GITBOOK_VISITOR_SIGNING_KEY));
    
    // 将用户重定向到 URL 中包含 JWT 令牌的 GitBook
    const redirectURL = `${GITBOOK_DOCS_URL}/?jwt_token=${gitbookVisitorJWT}`;
    res.redirect(redirectURL);
}
```

{% endcode %}

在设置并配置好要发送给 GitBook 的正确 claims 后，前往“[为你的内容添加适配](/docs/documentation/zh/zhan-dian-fang-wen/adaptive-content/adapting-your-content.md)”继续配置你的站点。


---

# Agent Instructions: 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:

```
GET https://gitbook.com/docs/documentation/zh/zhan-dian-fang-wen/authenticated-access/setting-up-a-custom-backend.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
