跳转到内容
本站为社区维护的非官方中文翻译,与 Cloudflare, Inc. 无关联。本站不提供账户登录,请勿在任何非官方站点输入您的 Cloudflare 账户信息。官方文档:developers.cloudflare.com

显示辅导页

最后更新 查看 MarkdownAgent 设置

当用户访问所选页面时显示说明。Worker 返回合成响应而不调用 next(),因此 Gateway 不会将请求发送到原始目标。

此模板显示带有指向已批准替代方案链接的通知。它不实现临时例外、审批工作流,或解锁原始站点的确认。

配置 Worker

在 Worker 项目中,将 @cloudflare/zt-types ↗ 2026.10.1 或更高版本安装为开发依赖项:

npm i -D @cloudflare/zt-types

保留 src/index.ts 中 GatewayMiddlewareContext 和 GatewayMiddlewareNext 的 import type 声明。它们描述 Gateway 上下文和延续函数,并会在 TypeScript 编译时被移除。从 cloudflare:workers 导入运行时 WorkerEntrypoint 类。

将此脚本用作 Worker 项目中的 src/index.ts。将已批准的应用程序 URL 和说明替换为你组织的指导。它不需要绑定。

src/index.jsjs
import { WorkerEntrypoint } from "cloudflare:workers";

export class GatewayMiddleware extends WorkerEntrypoint {
	async handle(request, context, next) {
		if (
			request.method !== "GET" ||
			!request.headers.get("accept")?.includes("text/html")
		) {
			return next(request);
		}
		return new Response(
			`<!doctype html>
      <html lang="en"><meta charset="utf-8">
      <title>Use the approved application</title>
      <h1>Keep work data in the approved application</h1>
      <p>Use your company workspace when sharing work files.</p>
      <p><a href="https://approved.example.com/">Open your company workspace</a></p>
      </html>`,
			{
				status: 200,
				headers: {
					"content-type": "text/html; charset=utf-8",
					"cache-control": "no-store",
					"content-security-policy":
						"default-src 'none'; base-uri 'none'; frame-ancestors 'none'",
					"x-content-type-options": "nosniff",
				},
			},
		);
	}
}
src/index.tsts
import { WorkerEntrypoint } from "cloudflare:workers";
import type {
	GatewayMiddlewareContext,
	GatewayMiddlewareNext,
} from "@cloudflare/zt-types";

export class GatewayMiddleware extends WorkerEntrypoint {
	async handle(
		request: Request,
		context: GatewayMiddlewareContext,
		next: GatewayMiddlewareNext,
	): Promise<Response> {
		if (
			request.method !== "GET" ||
			!request.headers.get("accept")?.includes("text/html")
		) {
			return next(request);
		}
		return new Response(
			`<!doctype html>
      <html lang="en"><meta charset="utf-8">
      <title>Use the approved application</title>
      <h1>Keep work data in the approved application</h1>
      <p>Use your company workspace when sharing work files.</p>
      <p><a href="https://approved.example.com/">Open your company workspace</a></p>
      </html>`,
			{
				status: 200,
				headers: {
					"content-type": "text/html; charset=utf-8",
					"cache-control": "no-store",
					"content-security-policy":
						"default-src 'none'; base-uri 'none'; frame-ancestors 'none'",
					"x-content-type-options": "nosniff",
				},
			},
		);
	}
}

转发并测试

部署 Worker,并为一个测试主机、所选页面路径、HTTP Method GET 以及测试用户组创建 Forward 策略。将已批准的替代方案保持在此策略范围之外。

在浏览器中访问所选页面,并确认出现辅导页。检查测试源站的日志,确认此次访问没有发出上游请求。没有 HTML Accept 标头的请求会正常通过。

方法和 Accept 检查用于选择何时显示页面。它们不是访问控制,因为客户端可以更改请求标头。

当你需要阻止访问时,请使用 Gateway Block 策略。带有 Block 操作的 HTTP 请求策略会阻止调用 Worker,而来自请求体策略或检查的 Block 决定可以中断 Worker。

合成响应仍会经过 Gateway 响应检查。

这篇文档对您有帮助吗?