在Solo-io Gloo中使用Google OAuth实现应用认证
2025-06-12 17:16:36作者:乔或婵
前言
在现代微服务架构中,应用安全认证是不可或缺的一环。本文将详细介绍如何在Solo-io Gloo网关中配置Google OAuth认证,为您的应用添加基于OpenID Connect(OIDC)的身份验证功能。
准备工作
环境要求
在开始之前,请确保您已具备以下条件:
- 已部署Solo-io Gloo网关
- 拥有Google开发者账号
- 本地开发环境已配置kubectl工具
注意事项
由于Gloo网关的外部认证服务需要与OIDC提供者(Google)进行交互,认证流程可能会超过默认的200ms超时时间。您可以通过修改外部认证设置中的requestTimeout值来延长超时时间。
部署示例应用
我们将使用一个简单的宠物商店应用作为演示:
kubectl apply -f [petstore应用YAML文件地址]
创建虚拟服务
首先,我们需要创建一个虚拟服务(VirtualService)来路由请求:
apiVersion: gateway.solo.io/v1
kind: VirtualService
metadata:
name: default
namespace: gloo-system
spec:
virtualHost:
domains:
- '*'
routes:
- matchers:
- exact: /all-pets
options:
prefixRewrite: /api/pets
routeAction:
single:
upstream:
name: default-petstore-8080
namespace: gloo-system
验证服务是否正常工作:
kubectl -n gloo-system port-forward svc/gateway-proxy 8080:80
访问http://localhost:8080/all-pets应该能看到宠物列表。
配置Google OAuth认证
1. 在Google开发者控制台注册应用
- 登录Google开发者控制台
- 创建新项目(如果是首次使用)
- 导航至"OAuth同意屏幕"菜单
- 输入应用名称并选择"内部"作为应用类型
- 保存设置
- 导航至"凭据"菜单
- 创建OAuth客户端ID,选择"Web应用"类型
- 添加授权重定向URI:
http://localhost:8080/callback - 记录下生成的客户端ID和客户端密钥
2. 创建客户端密钥Secret
将客户端密钥存储在Kubernetes Secret中:
glooctl create secret oauth --namespace gloo-system --name google --client-secret $CLIENT_SECRET
3. 创建AuthConfig资源
apiVersion: enterprise.gloo.solo.io/v1
kind: AuthConfig
metadata:
name: google-oidc
namespace: gloo-system
spec:
configs:
- oauth2:
oidcAuthorizationCode:
appUrl: http://localhost:8080
callbackPath: /callback
clientId: $CLIENT_ID
clientSecretRef:
name: google
namespace: gloo-system
issuerUrl: https://accounts.google.com
session:
cookieOptions:
notSecure: true
scopes:
- email
注意:此配置中我们明确允许了不安全的cookie(notSecure: true),仅用于本地开发测试。在生产环境中应使用HTTPS并禁用此选项。
4. 更新虚拟服务
将认证配置应用到虚拟服务:
apiVersion: gateway.solo.io/v1
kind: VirtualService
metadata:
name: default
namespace: gloo-system
spec:
virtualHost:
domains:
- '*'
routes:
- matchers:
- prefix: /callback
options:
prefixRewrite: '/login'
routeAction:
single:
upstream:
name: default-petstore-8080
namespace: gloo-system
- matchers:
- exact: /all-pets
options:
prefixRewrite: /api/pets
routeAction:
single:
upstream:
name: default-petstore-8080
namespace: gloo-system
options:
extauth:
configRef:
name: google-oidc
namespace: gloo-system
测试认证流程
- 启动端口转发:
kubectl port-forward -n gloo-system deploy/gateway-proxy 8080 &
portForwardPid=$!
- 访问
http://localhost:8080/all-pets,应该会被重定向到Google登录页面 - 登录后,将被重定向回应用,并在URL中包含Google提供的认证信息
故障排查
如果认证失败,可以检查外部认证服务的日志:
kubectl logs -n gloo-system deploy/extauth -f
成功加载配置后,您应该能看到日志行:
"logger":"extauth","caller":"runner/run.go:179","msg":"got new config"
清理资源
完成测试后,可以删除创建的资源:
kill $portForwardPid
kubectl delete virtualservice -n gloo-system default
kubectl delete authconfig -n gloo-system google-oidc
kubectl delete secret -n gloo-system google
kubectl delete -f [petstore应用YAML文件地址]
总结
通过本文,您已经学会了如何在Solo-io Gloo网关中配置Google OAuth认证。这种认证方式不仅提供了安全的用户认证机制,还能利用Google庞大的用户基础,为您的应用提供便捷的登录体验。在实际生产环境中,您可能需要考虑添加更多的安全措施,如HTTPS、CSRF保护等。
登录后查看全文
热门项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0215
cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。Jupyter Notebook0138
uni-appA cross-platform framework using Vue.jsJavaScript08
GLM-5.2智谱开源 GLM-5.2,这是针对长文本任务的最新旗舰模型。相较于前代产品 GLM-5.1,它在长文本任务处理能力上实现了显著飞跃,并且首次在稳定的 100 万 token 上下文中提供这一能力。Jinja00
SwanLab⚡️SwanLab - an open-source, modern-design AI training tracking and visualization tool. Supports Cloud / Self-hosted use. Integrated with PyTorch / Transformers / LLaMA Factory / veRL/ Swift / Ultralytics / MMEngine / Keras etc.Python00
tiny-universe《大模型白盒子构建指南》:一个全手搓的Tiny-UniverseJupyter Notebook03
项目优选
收起
deepin linux kernel
C
32
16
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
471
465
暂无描述
Dockerfile
779
5.08 K
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。
C++
877
2.03 K
Ascend Extension for PyTorch
Python
758
968
本项目是CANN提供的神经网络类计算算子库,实现网络在NPU上加速计算。
C++
698
1.4 K
昇腾LLM分布式训练框架
Python
185
231
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
1.1 K
1.14 K
本仓库是 Flutter SDK 与 Flutter Engine 的 OpenHarmony 适配版本,由 CPF-Flutter 团队维护。开发者可使用熟悉的 Flutter 技术栈开发 OpenHarmony 应用,3.35.7 及以后的适配版本可基于本仓库源码构建支持 OpenHarmony 的 Flutter Engine。
Dart
1.04 K
271
JiuwenSwarm 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。
Python
2.25 K
677