Keycloak无效重定向uri与Couchbase同步网关OpenID连接Nginx

时间:2017-06-13 07:56:53

标签: nginx reverse-proxy openid-connect keycloak couchbase-sync-gateway

我无法在Keycloak服务器和Couchbase Sync Gateway之间连接OpenID Connect。我的设置如下:我有一个nginx,它为Keycloak和Sync Gateway提供SSL终止和反向代理。所以我的keycloak认证地址如下:

https://auth.domain.com

我的Sync Gateway存储桶位于:

https://sg.domain.com/sync_gateway

我已使用授权码在keycloak中设置了一个机密客户端,其重定向网址为:

https://sg.domain.com/sync_gateway/_oidc_callback

我在Couchbase Lite for .NET中使用内置的OpenIDConnectAuthenticator。当我的应用程序将用户带到Keycloak登录页面时,我得到:

Invalid parameter: redirect_uri

传递给我的应用程序的登录URL是:

https://auth.domain.com/auth/realms/realm/protocol/openid-connect/auth?access_type=offline&client_id=couchbase-sync-gateway&prompt=consent&redirect_uri=http%3A%2F%2Fsg.domain.com%2Fsync_gateway%2F_oidc_callback&response_type=code&scope=openid+email&state=

我可以看到redirect_uri是http。它应该是https。

我的Sync Gateway配置为:

{
  "log": ["*"],
  "databases": {
    "sync_gateway": {
      "server": "http://cbserver:8091",
      "bucket": "sync_gateway",
      "users": { "GUEST": { "disabled": true, "admin_channels": ["*"] } },
      "oidc": {
        "providers": {
          "keycloakauthcode": {
            "issuer":"https://auth.domain.com/auth/realms/realm",
            "client_id":"couchbase-sync-gateway",
            "validation_key":"myclientid",
            "register":true
          }
        }
      }
    }
  }
}

我的nginx配置是:

events {
    worker_connections 768;
    multi_accept on;
}
http {
    proxy_buffer_size 128k;
    proxy_buffers 4 256k;
    proxy_busy_buffers_size 256k;
    large_client_header_buffers  4 32k;        

    upstream auth_backend { 
        server server1:port1;
    }
    upstream cb_sync_gateway { 
        server server2:port2;
    }
    server { # AUTH 

        listen 443 ssl;
        server_name auth.domain.com;

        ssl on;
        ssl_certificate /local/ssl/domain_com.crt;
        ssl_certificate_key /local/ssl/domain_com.key;

        add_header Content-Security-Policy upgrade-insecure-requests;

        location / {
            proxy_http_version 1.1;
            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 https;

            proxy_pass       http://auth_backend;
        }
    }

    server {
        listen 443 ssl;
        server_name sg.domain.com;

        ssl on;
        ssl_certificate /local/ssl/domain_com.crt;
        ssl_certificate_key /local/ssl/domain_com.key;

        add_header Content-Security-Policy upgrade-insecure-requests;

        location / {
            proxy_http_version 1.1;
            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 https;

            proxy_pass       http://cb_sync_gateway;
        }
    }
}

Keycloak standalone-ha.xml具有代理设置:https://github.com/ak1394/keycloak-dockerfiles

我不确定这是与nginx设置还是keycloak设置有关。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我能解决这个问题;可能不是最好的方式,但它现在正在工作。我还需要在nginx配置中设置:

proxy_redirect http:// https://

并在Keycloak中,添加以下有效的重定向网址:

http://sg.domain.com/sync_gateway/_oidc_callback

如果有人在没有不安全的有效重定向的情况下找到了这样做的方法,我会非常想知道,因为我知道不建议这样做。

修改

我已发布在Couchbase Forums,好像它可能是Couchbase Mobile(Coucbase Lite或Sync Gateway)中的错误。他们在Couchbase Lite for .NET中提交了ticket