#为 Cloudflare Access 配置 Google 登录

Cloudflare Access 是 Cloudflare Zero Trust 提供的身份验证网关,可以保护自托管服务,让只有授权用户才能访问。本文介绍如何将 Google 账号接入 Cloudflare Access,实现一键 Google 登录。

#前置条件

  • 已有 Cloudflare 账号,并开通 Zero Trust(免费版即可)
  • 已有 Google 账号(不需要 Google Workspace)
  • 已在 Cloudflare Access 中创建好需要保护的 Application

#Step 1:找到你的 Team Name

  1. 进入 https://one.dash.cloudflare.com
  2. 左侧菜单 → SettingsTeam name and domain
  3. 记下你的 Team name,格式类似 your-team-name

后续配置 Google OAuth 时会用到,完整域名格式为:

CODE
1https://<your-team-name>.cloudflareaccess.com

#Step 2:在 Google Cloud Console 创建 OAuth 应用

#2.1 新建项目

  1. 打开 https://console.cloud.google.com
  2. 顶部项目选择器 → New Project
  3. 填入项目名称(如 Cloudflare Access)→ Create

#2.2 配置 OAuth 同意屏幕

  1. 左侧菜单 → APIs & ServicesCredentials
  2. 点击 Configure Consent Screen
  3. 点击 Get started,填写以下信息:
    • App name:随便填,如 Cloudflare Access
    • User support email:填你的 Gmail 地址
    • Audience:选 External(允许任意 Google 账号登录)
    • Contact email:同上
  4. 同意 Google 用户数据政策 → Create

#2.3 创建 OAuth Client ID

  1. 回到 Credentials 页面
  2. 点击 Create OAuth client
  3. Application type 选择 Web application
  4. 填写名称(随便填)
  5. Authorized JavaScript origins 添加:
    CODE
    1https://<your-team-name>.cloudflareaccess.com
  6. Authorized redirect URIs 添加:
    CODE
    1https://<your-team-name>.cloudflareaccess.com/cdn-cgi/access/callback
  7. 点击 Create

创建成功后,Google 会显示:

  • Client ID(即 App ID)
  • Client Secret

妥善保存这两个值,后续需要填入 Cloudflare。


#Step 3:在 Cloudflare 添加 Google Identity Provider

  1. 进入 https://one.dash.cloudflare.com
  2. 左侧 SettingsAuthenticationLogin methods
  3. 点击 Add new → 选择 Google
  4. 填写以下信息:
    • NameGoogle
    • App ID:填入上一步的 Client ID
    • Client secret:填入上一步的 Client Secret
  5. 点击 Save
  6. 点击 Test 验证连接是否正常 → 浏览器会弹出 Google 登录页,登录成功后显示 ✅

#Step 4:在 Application 中启用 Google 登录

  1. 左侧 AccessApplications → 找到你的应用 → Edit
  2. 切换到 Login methods 标签
  3. 关闭 Accept all available identity providers
  4. 手动勾选 Google
  5. (可选)如果只使用 Google 登录,开启 Apply instant authentication,跳过登录方式选择页面,直接跳转 Google
  6. Save application

#Step 5:更新 Policy(可选)

如果你希望只允许特定 Google 账号访问,在 Policy 中设置:

  • IncludeEmails → 填入允许的邮箱地址

如果希望允许某个域名下的所有 Google 账号(如公司邮箱),可以使用:

  • IncludeEmail domain → 填入 yourcompany.com

#效果

配置完成后,用户访问受保护的应用时,会看到 Cloudflare Access 的登录页面,点击 Sign in with Google 完成授权即可进入。

整个过程对用户透明,体验和普通 Google 登录一致。


#注意事项

  • Google OAuth 应用默认处于"测试"模式,个人使用完全够用,不需要发布审核
  • Client Secret 相当于密码,不要泄露或提交到代码仓库
  • 如果后续删除了 Google Cloud 中的 OAuth Client,会报 Error 401: deleted_client,需要重新创建并更新 Cloudflare 中的配置