🌟

keycloak 介绍和搭建

Keycloak是一个开源身份和访问管理 (IAM) 平台。它完全支持 OpenID Connect (OIDC)SAML 2.0 这两大主流认证协议框架。
 
 
我们将使用两个 Azure PaaS 服务:
  • Azure Database for PostgreSQL (灵活服务器):用于存储数据。
  • Azure Container App (容器应用):用于运行 Keycloak 容器,且内置了 HTTPS 和公共域名

1. Container App创建Keycloak

🛍️
详细配置步骤
重点配置环境变量
notion image
且问题点,必须在ingress中勾选Insecure connections,容器才能跑启来:
核心原因:Azure 是个“剥皮”的中间人
  • 在 Azure 层面:当用户的请求(HTTPS)到达 Azure Container Apps 的负载均衡器时,Azure 负责处理加密解密。为了提高性能,Azure 此时会把 HTTPS 的“外壳”剥掉。
  • 在容器内部:Azure 把剥掉加密壳的请求,以 HTTP(明文) 的形式转发给 Keycloak 容器。
notion image
完成后登录:
notion image
notion image

2. keycloak portal创建用户实现登录

notion image
notion image
notion image
在没有分配Role之前,默认普通用户无法登录管理员后台
notion image
notion image
分配完之后,登录成功
notion image
 

3. 为keycloak添加自定义域名

💡
为什么一定需要绑定自定义域名? Entra ID 要求重定向 URL (Redirect URI) 必须是安全的 https 地址。虽然它允许 localhost 用于开发,但一旦上线,不能使用裸 IP 地址(如 https://123.45.67.89/)作为回调地址。
notion image
DNS中添加 A 和 TXT 记录
notion image
notion image
notion image
域名已经认领了,但 SSL 证书(HTTPS 安全锁)还没有挂上去,在 Azure Container Apps (ACA) 中,证书的管理层级是在 Environment (环境) 这一层,而不是 App 这一层,所以点击To add a certificate, go to the Container Apps Environment
notion image
notion image
succeed之后再返回app
notion image
现在验证访问,直接在浏览器打开:https://sso.stat200.net
  • 预期结果:能看到 Keycloak 的欢迎页面(Welcome to Keycloak)。
  • 注意:如果点击 "Administration Console" 后跳转回了原来的长域名或者 IP,或者页面样式加载不出来,请必须执行第二步
💡
Keycloak 默认不知道自己变身成了 sso.stat200.net。必须修改容器的环境变量,否则生成的登录链接(Redirect URI)还是旧的,导致微软那边报错。 1. 回到 Azure Container Apps -> Containers -> Edit and deploy。 2. 编辑 Keycloak 容器,修改/添加以下环境变量:变量名值说明KC_HOSTNAMEsso.stat200.net核心配置。告诉 Keycloak 生成链接时用这个域名。KC_PROXYedge告诉 Keycloak 信任 Azure 转发进来的流量,并识别为 HTTPS。KC_HOSTNAME_STRICTfalse(建议先设为 false 防止调试时无法访问后台,跑通后再改 true)
notion image
报错
notion image
notion image
Revision mode: Multiple (多修订版模式)。 在这种模式下,Azure 为了安全起见,当发布新版本(19:30 的那个)时,它不会自动把流量切过去,而是先把新版本启动起来(Running),但让它处于“待命”状态(Traffic 0%),防止新版本有 Bug 导致服务挂掉,所以需要手动修改
  • 将第一行(最新版,19:30那个)的 Traffic 输入框从 0 改为 100
  • 将第二行(旧版本,17:55那个)的 Traffic 输入框从 100 改为 0
notion image
notion image
notion image