反向代理后面的keycloak重定向

keycloak redirect behind reverse proxy

我正试图让 keycloak 到 运行 在 kubernetes 的 kong ingress 后面。我遇到的问题是 keycloak 正在添加一个它认为它在它返回的 url 上的端口。 要求:auth.mydomain.com/auth 回应:auth.mydomain.com:8443/auth/admin/master/console/

客户端 -> NLB:443 -> kong:8443 -> keycloak:8080

keycloak 看到 kong 设置的请求 x-forwarded-port: 8443 并在端口 8443

上回复路径

Kong 现在必须在 8443 上,因为它是在用户 kong 下的容器中启动的,并且无法访问特权端口。

我已经设置了 keycloak 建议的配置,

/socket-binding-group=standard-sockets/socket-binding=proxy-https:add(port=443)
/subsystem=undertow/server=default-server/http-listener=default:write-attribute(name=redirect-socket, value=proxy-https)
/subsystem=undertow/server=default-server/http-listener=default:write-attribute(name=proxy-address-forwarding, value=true)

但是这些看起来像是告诉 undertow 如果请求以纯文本形式出现则用 443 回复,我的 x-forwarded-proto: https 设置正确。

也有可能是 host header 问题。

----------------------------REQUEST---------------------------
               URI=/auth/admin/
 characterEncoding=null
     contentLength=-1
       contentType=null
            header=X-Real-IP=<REDACTED>
            header=Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
            header=Accept-Language=en-us
            header=Accept-Encoding=br, gzip, deflate
            header=User-Agent=<REDACTED>
            header=Connection=keep-alive
            header=X-Forwarded-Proto=https
            header=X-Forwarded-Port=8443
            header=X-Forwarded-For=<REDACTED>
            header=Referer=https://auth.my-domain.com/auth/
            header=Host=auth.my-domain.com:8443
            header=X-Forwarded-Host=auth.my-domain.com
            locale=[en_US]
            method=GET
          protocol=HTTP/1.1
       queryString=
        remoteAddr=/<REDACTED>
        remoteHost=<REDACTED>
            scheme=https
              host=auth.my-domain.com:8443
        serverPort=8443
          isSecure=true
--------------------------RESPONSE--------------------------
     contentLength=0
       contentType=null
            header=Connection=keep-alive
            header=Location=https://auth.my-domain.com:8443/auth/admin/master/console/
            header=Content-Length=0
            header=Date=Fri, 21 Jun 2019 07:05:28 GMT
            status=302

如果我能让 keycloak 仅使用路径而不是端口进行响应,或者如果它必须使用端口进行响应,只需告诉它我希望它在哪个端口上进行通话,那就太好了。

hostname SPI,它允许您为 Keycloak 设置主机名和端口,而不是从 HTTP 请求中检索它们。您可以将端口设置为默认端口 (80/443),生成的 URL 将不会显示端口。

# Replace `request` provider with `fixed`
/subsystem=keycloak-server/spi=hostname:write-attribute(name=default-provider,value=fixed)
# Set hostname and ports for http and https
/subsystem=keycloak-server/spi=hostname/provider=fixed:write-attribute(name=properties.hostname,value=example.org)
/subsystem=keycloak-server/spi=hostname/provider=fixed:write-attribute(name=properties.httpsPort,value=443)
/subsystem=keycloak-server/spi=hostname/provider=fixed:write-attribute(name=properties.httpPort,value=80)

这些值在运行时不可配置,因此您必须在更改后重新启动 Keycloak。