KeyCloak重定向问题(Kong)背后

发布于 2025-02-11 18:17:58 字数 1262 浏览 2 评论 0原文

我正在尝试设置一个KeyCloak实例来处理我的WebApp用户。与所有其他微服务一样,此实例隐藏在反向代理后面(Kong,它是基于NGINX的代理)。

在我的本地设置中,Kong会聆听https:// localhost,而KeyCloak会听http:// localhost:8082/auth。 为此,我在KeyCloak容器上使用了几个环境变量:

ENV KC_HOSTNAME=localhost
ENV KC_HOSTNAME_PORT=8082
ENV KC_HOSTNAME_STRICT_HTTPS=false
ENV KC_PROXY=edge
ENV PROXY_ADDRESS_FORWARDING=true
ENV KC_HTTP_ENABLED=true
ENV KC_HTTP_PORT=8082
KC_HTTP_RELATIVE_PATH=/auth

Kong配置的设置看起来不错,并且我需要的KeyCloak端点可以通过Kong正确公开(/code>/code/js/js ,/resources/robots.txt,就像Doc所说)。 Kong处理TLS连接,然后仅使用http对所有微服务进行交谈,因此kc_proxy = edge/admin没有暴露,我可以在右机器上使用localhost:8082在本地访问此问题。

如果我转到https://localhost/auth/realms/master/.well-nown/openid-configuration,我会得到配置。但是,KeyCloak不知道它在Kong背后,因此所有端点都包含localhost:8082。这似乎是正常的,因为这是我首先设置它的方式。

我尝试使用不同的前端url添加新的ronem,称其为https://myapp.com 现在,我的OpenID配置包含https://myapp.com:8082/...到处都是。所有的工作流都会出现错误的URL。 我想念什么?我无法删除我首先放置的此端口,否则我将无法访问管理员控制台。 我以为我可以用kc_hostname_admin做点事,但是不幸的是没有kc_hostname_admin_port ..或在那里?

谢谢您的阅读:)

I'm trying to setup a Keycloak instance to handle the users of my webapp. This instance would be, like all others microservices, hidden behind a reverse proxy (Kong, it's a nginx-based proxy).

On my local setup, Kong listens to https://localhost, and keycloak listens to http://localhost:8082/auth.
To achieve that, I used several environment variables on my Keycloak container :

ENV KC_HOSTNAME=localhost
ENV KC_HOSTNAME_PORT=8082
ENV KC_HOSTNAME_STRICT_HTTPS=false
ENV KC_PROXY=edge
ENV PROXY_ADDRESS_FORWARDING=true
ENV KC_HTTP_ENABLED=true
ENV KC_HTTP_PORT=8082
KC_HTTP_RELATIVE_PATH=/auth

The setup of Kong configuration looks fine, and the keycloak endpoints that I need are exposed correctly through Kong (/realms, /js, /resources, /robots.txt, like the doc said). Kong handles the TLS connection, and then speaks to all microservices with HTTP only, thus KC_PROXY=edge. /admin is not exposed, I though I could access this locally using localhost:8082 on the right machine.

If I go to https://localhost/auth/realms/master/.well-known/openid-configuration, I get the configuration. However, Keycloak does not know it's behind Kong, so all endpoints contains localhost:8082. That seems normal, since it's how I set it up in the first place.

I tried to add a new realm with a different Frontend URL, calling it https://myapp.com
Now, my openid configuration contains https://myapp.com:8082/... everywhere. All the workflows get wrongs URLs.
What did I miss ? I cannot remove this port that I put in the first place, otherwise I will not be able to access the admin console.
I thought I could do something with KC_HOSTNAME_ADMIN, but unfortunately there is no KC_HOSTNAME_ADMIN_PORT.. or is there ?

Thank you for reading :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2025-02-18 18:17:58

如果某人感兴趣,那么解决方案实际上很简单。首先,我不应该设置hostName hostname_port 。
ENV KC_HOSTNAME_STRICT_HTTPS = FALSE是强制性的,我还需要在Kong中添加一个插件以调整标头:

plugins
- name: post-function
  service: keycloak
  config:
    functions:
      - return function()
          if ngx.var.upstream_x_forwarded_port == "8000" then
            ngx.var.upstream_x_forwarded_port = 80
          elseif ngx.var.upstream_x_forwarded_port == "8443" then
            ngx.var.upstream_x_forwarded_port = 443
          end
        end

否则,KeyCloak在某些情况下会有错误的重定向URI。

In case it's of interest to someone, the solution was actually quite simple. I should not have set the HOSTNAME and HOSTNAME_PORT in the first place.
ENV KC_HOSTNAME_STRICT_HTTPS=false is mandatory, and also I needed to add a plugin to Kong to tweak the headers :

plugins
- name: post-function
  service: keycloak
  config:
    functions:
      - return function()
          if ngx.var.upstream_x_forwarded_port == "8000" then
            ngx.var.upstream_x_forwarded_port = 80
          elseif ngx.var.upstream_x_forwarded_port == "8443" then
            ngx.var.upstream_x_forwarded_port = 443
          end
        end

Otherwise, keycloak would have the wrong redirect uri in some cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文