【问题标题】:Keycloak Redirect URI is adding port zero to the urlKeycloak Redirect URI 正在将端口零添加到 url
【发布时间】:2018-12-09 19:11:41
【问题描述】:

在 keycloak 中遇到 redirect_uri 错误。发现在 JIRA KEYCLOAK-7237 上记录了相同的问题,只是想检查任何解决方法吗?任何人都可以帮忙吗?提前谢谢你。

2018-06-30 11:34:13,996 WARN [org.keycloak.events](默认任务 8)type=LOGIN_ERROR,realmId=Victz,clientId=portal,userId=null,ipAddress=,error=invalid_redirect_uri, redirect_uri=https://www.example.com:0/home

我正在使用在centos7 上运行的apache http 反向代理,wildly 10,keycloak 3.4.3。在下面的环境中也试过,但同样的错误。

试用过 狂野 10,狂野 11,jboss 7.1, Keycloak 3.4.3 和 keycloak 4.0

还尝试关闭 apache http 并直接访问 http://www.example.org:8080/home ,但似乎 return_uri 自动转换为端口 0 的 https。

请看下面的standalone.xml,尝试在proxy-peer 和request-dumper 配置下删除,但没有成功。

    <subsystem xmlns="urn:jboss:domain:undertow:4.0">
        <buffer-cache name="default"/>
        <server name="default-server">
            <http-listener name="default" socket-binding="http" proxy-address-forwarding="true" enable-http2="true"/>
            <https-listener name="https" socket-binding="https" proxy-address-forwarding="true" security-realm="ApplicationRealm" enable-http2="true"/>
            <host name="default-host" alias="localhost">
                <location name="/" handler="welcome-content"/>
                <location name="/drive" handler="drive"/>
                <access-log pattern="%h %l %u %t &quot;%r&quot; %s %b &quot;%{i,Referer}&quot; &quot;%{i,User-Agent}&quot; &quot;%{i,COOKIE}&quot; &quot;%{o,SET-COOKIE}&quot; %S &quot;%I %T&quot;" prefix="access."/>
                <filter-ref name="server-header"/>
                <filter-ref name="x-powered-by-header"/>
                <http-invoker security-realm="ApplicationRealm"/>
            </host>
            <host name="example1" alias="example.com1,www.example.com1" default-web-module=“example1-0.1.war">
                <location name="/drive" handler="drive”/>
                <filter-ref name="proxy-peer"/>
                <filter-ref name="request-dumper" priority="30"/>
            </host>
            <host name="example2" alias="example.com2,www.example.com2" default-web-module="example2-0.1.war">
                <location name="/drive" handler="drive"/>
                <filter-ref name="proxy-peer"/>
                <filter-ref name="request-dumper" priority="30"/>
            </host>
            <host name="example3" alias="example.com3,www.example.com3" default-web-module="example3-0.1.war">
                <location name="/drive" handler="drive"/>
                <filter-ref name="proxy-peer"/>
                <filter-ref name="request-dumper" priority="30"/>
            </host>

        </server>
        <servlet-container name="default">
            <jsp-config/>
            <websockets/>
        </servlet-container>
        <handlers>
            <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
            <file name="drive" path="/app/drive"/>
        </handlers>
        <filters>
            <response-header name="server-header" header-name="Server" header-value="JBoss-EAP/7"/>
            <response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/>
            <filter name="proxy-peer" class-name="io.undertow.server.handlers.ProxyPeerAddressHandler" module="io.undertow.core"/>
            <filter name="request-dumper" class-name="io.undertow.server.handlers.RequestDumpingHandler" module="io.undertow.core"/>
        </filters>
    </subsystem>

【问题讨论】:

  • 我也有同样的问题。我不知道 :0 端口是来自 java 适配器(在我的情况下是 keycloak 代理)还是来自 keycloak 中配置错误的客户端。在 keycloak 侦听器中还有 proxy-address-forwarding="true" 选项,至少在 keycloak 代理中是 proxy-address-forwarding 参数。但这会影响主机转发的行为,而不是端口。 Keycloak 正在监听 https:8443,keycloakproxy 在 http:8080
  • @Shawn - 你明白了吗?我有同样的问题。我在 centos 7 上使用 nginx 反向代理。

标签: reverse-proxy keycloak wildfly-10 undertow wildfly-11


【解决方案1】:

我遇到了类似的问题。为了使它工作,我更新了standalone.xml,如下所示

服务器配置

<http-listener name="default" socket-binding="http" allow-unescaped-characters-in-url="true" redirect-socket="https" proxy-address-forwarding="true" enable-http2="true"/>

SSO 配置

<subsystem xmlns="urn:jboss:domain:keycloak:1.1">
<realm name="myrealm">
<auth-server-url>https://sso.example.com:443/auth</auth-server-url>
<ssl-required>external</ssl-required>
<enable-cors>true</enable-cors>
<confidential-port>0</confidential-port>
</realm>
<secure-deployment name="mywar.war">
<realm>myrealm</realm>
<ssl-required>external</ssl-required>
<resource>myclient</resource>
<public-client>true</public-client>
</secure-deployment>
</subsystem>

【讨论】:

    【解决方案2】:

    上下文

    在反向代理后面使用Keycloak的Spring Boot client adapter时,OAuthRequestAuthenticator.java设置的URL查询参数redirect_uri可能会因为配置不完整或错误而包含端口后缀:0

    基本问题

    为了构造redirect_uri,Spring Boot 应用程序需要猜测它的公共 URL,至少包括协议和主机名,可能还包括 IP 端口。在反向代理之后,这需要与代理合作和/或显式配置。

    :0 的解释

    :0,如果存在,可能是由OAuthRequestAuthenticator.java 设置的(这种松散的处理可能被认为是一个错误,但在目前的情况下源于不完整的配置。):Keycloak 识别出 Spring Boot 应用程序正在使用 HTTPS ,但是没有发现HTTPS端口配置,所以默认为零。

    解决步骤

    假设:

    • 这是关于 Spring Boot 应用的
    • 在反向代理后面运行
    • 充当 Keycloak 客户端

    您需要确保以下事项来解决问题:

    1. 配置反向代理以添加以下标头
      • x-forwarded-for 告诉主机名
      • x-forwarded-proto 判断连接是否安全
    2. 配置 Spring Boot 应用程序以正确运行
    3. 配置Keycloak's Spring Boot adapter
      • 也许您需要将confidential-port 显式设置为443

    故障排除/交叉检查

    只有一点:假设这是关于反向代理后面的 Spring Boot 应用程序,您应该能够使用 tcpdump 或类似的工具来监视未加密的流量(如 -i lo -s0 -A)并查看 HTTP 标头。发送到 Spring Boot 应用程序的任何请求都应具有上述 Spring Boot 安全操作指南(x-forwarded-forx-forwarded-proto)所需的标头,否则,代理配置错误。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题。我的 Spring Boot 应用程序位于 nginx 后面。我更新了 nginx 以通过 x-forwarded 标头并使用

      更新了 Spring Boot 配置

      spring boot yaml 配置:

      server:
        use-forward-headers: true    
      
      keycloak:
        realm: myrealm
        public-client: true
        resource: myclient
        auth-server-url: https://sso.example.com:443/auth
        ssl-required: external
        confidential-port: 443
      

      nginx 配置:

      upstream app {
         server 1.2.3.4:8042 max_fails=1 fail_timeout=60s;
         server 1.2.3.5:8042 max_fails=1 fail_timeout=60s;
      }
      
      server {
          listen 443;
          server_name www.example.com;
      
          ...
      
          location / {
              proxy_set_header        Host $host;
              proxy_set_header        X-Real-IP $remote_addr;
              proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header        X-Forwarded-Proto $scheme;
              proxy_set_header        X-Forwarded-Host $host;
              proxy_set_header        X-Forwarded-Port   443;
      
              proxy_next_upstream     error timeout invalid_header http_500;
              proxy_connect_timeout   2;
      
              proxy_pass          http://app;
          }
      }
      

      使它对我有用的具体更改是添加keycloak.confidential-port。一旦我添加它不再在 redirect_uri 中添加端口 0。

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-10
        • 1970-01-01
        • 2017-12-29
        • 1970-01-01
        • 2020-12-15
        • 2022-08-24
        • 2021-02-03
        相关资源
        最近更新 更多