#为 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
- 进入 https://one.dash.cloudflare.com
- 左侧菜单 → Settings → Team name and domain
- 记下你的 Team name,格式类似
your-team-name
后续配置 Google OAuth 时会用到,完整域名格式为:
1https://<your-team-name>.cloudflareaccess.com#Step 2:在 Google Cloud Console 创建 OAuth 应用
#2.1 新建项目
- 打开 https://console.cloud.google.com
- 顶部项目选择器 → New Project
- 填入项目名称(如
Cloudflare Access)→ Create
#2.2 配置 OAuth 同意屏幕
- 左侧菜单 → APIs & Services → Credentials
- 点击 Configure Consent Screen
- 点击 Get started,填写以下信息:
- App name:随便填,如
Cloudflare Access - User support email:填你的 Gmail 地址
- Audience:选 External(允许任意 Google 账号登录)
- Contact email:同上
- App name:随便填,如
- 同意 Google 用户数据政策 → Create
#2.3 创建 OAuth Client ID
- 回到 Credentials 页面
- 点击 Create OAuth client
- Application type 选择 Web application
- 填写名称(随便填)
- Authorized JavaScript origins 添加:CODE
1https://<your-team-name>.cloudflareaccess.com - Authorized redirect URIs 添加:CODE
1https://<your-team-name>.cloudflareaccess.com/cdn-cgi/access/callback - 点击 Create
创建成功后,Google 会显示:
- Client ID(即 App ID)
- Client Secret
妥善保存这两个值,后续需要填入 Cloudflare。
#Step 3:在 Cloudflare 添加 Google Identity Provider
- 进入 https://one.dash.cloudflare.com
- 左侧 Settings → Authentication → Login methods
- 点击 Add new → 选择 Google
- 填写以下信息:
- Name:
Google - App ID:填入上一步的 Client ID
- Client secret:填入上一步的 Client Secret
- Name:
- 点击 Save
- 点击 Test 验证连接是否正常 → 浏览器会弹出 Google 登录页,登录成功后显示 ✅
#Step 4:在 Application 中启用 Google 登录
- 左侧 Access → Applications → 找到你的应用 → Edit
- 切换到 Login methods 标签
- 关闭 Accept all available identity providers
- 手动勾选 Google
- (可选)如果只使用 Google 登录,开启 Apply instant authentication,跳过登录方式选择页面,直接跳转 Google
- Save application
#Step 5:更新 Policy(可选)
如果你希望只允许特定 Google 账号访问,在 Policy 中设置:
- Include → Emails → 填入允许的邮箱地址
如果希望允许某个域名下的所有 Google 账号(如公司邮箱),可以使用:
- Include → Email domain → 填入
yourcompany.com
#效果
配置完成后,用户访问受保护的应用时,会看到 Cloudflare Access 的登录页面,点击 Sign in with Google 完成授权即可进入。
整个过程对用户透明,体验和普通 Google 登录一致。
#注意事项
- Google OAuth 应用默认处于"测试"模式,个人使用完全够用,不需要发布审核
- Client Secret 相当于密码,不要泄露或提交到代码仓库
- 如果后续删除了 Google Cloud 中的 OAuth Client,会报
Error 401: deleted_client,需要重新创建并更新 Cloudflare 中的配置