JHipster keycloak docker配置用于生产

时间:2017-12-04 09:10:59

标签: jhipster keycloak

我的JHipster(v4.11.1)项目使用的是具有OAuth 2.0身份验证类型的Monolithic架构。我确实面临在生产服务器中托管它的几个问题。目前这里尚未确定:

点击“登录'链接我重定向到以下网址:

的https:// 我的网域名称 / AUTH /领域/ jhipster /协议/ OpenID的连接/ AUTH的client_id = web_app&安培; REDIRECT_URI = HTTP:// 我-搬运工-service-名称 /登录&安培; RESPONSE_TYPE =代码&安培;范围=的OpenID%20profile%20email&安培;状态= hO2NCQ

第一个问题是,我需要 my-docker-service-name my-domain-name (并使用https)。

注意:这里我看到keycloak登录页面,其中包含以下错误消息:Invalid parameter: redirect_uri

如果我手动将redirect_uri更改为我的域名,那么我会看到keycloak登录页面没有错误。下一个问题是我输入用户名/密码后,我会重定向到以下网址:

的http:// keycloak / AUTH /领域/ jhipster /登录-动作/认证码= 3MADiKg19-SL1L_lOmMEJv4w3kmGlF - 0hyIDInKPm8&安培;执行= 07cacbc6-5b72-407e-9a0c-9a1b6447a7ff&安培; CLIENT_ID = web_app

正如您所看到的,我的第二个问题是 keycloak 需要 my-domain-name (以及使用https)。

注意:如果将url手动更改为my-domain-name,则会看到登录页面,其中包含无效的用户名/密码错误消息。

此外,我在访问keycloak管理控制台方面遇到同样的问题(它会重定向到http:// keycloak )而且我看不到登录页面(Invalid parameter: redirect_uri

如果需要,我可以提供有关我的生产配置的更多信息吗?例如,我确实使用Nginx作为反向代理,也用于处理https请求。我的Nginx实例是一个docker容器,使用默认的docker网络来查找它的上游(keycloak(用于/auth路径)和my-app(用于/路径)。

即使我确实面对上述问题,到目前为止我对结果非常满意,我要感谢JHipster团队,Keycloak团队和Matt Raible! :-)使我们可以一起使用这个伟大的框架!干杯!

1 个答案:

答案 0 :(得分:0)

首先,this section(8.3. Setting Up a Load Balancer or Proxy) Keycloak文档绝对有用。我能够以某种方式使事情发挥作用但我仍然认为事情可以更好地以更安全的方式完成!

我不会为Keycloak方面重复所需的配置,但是如果您使用Nginx作为反向代理,我宁愿为您提供一些提示。

这是我的nginx.conf,其中包含所需的配置:

```

upstream rock-app {
    server rock-app:8080;
}
upstream keycloak {
    server keycloak:9080;
}

server {
    listen 80;
    listen 443 ssl http2;
    ...
    add_header Strict-Transport-Security "max-age=86400; includeSubdomains; preload" always;
    ...
}

location / {
    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://rock-app;
}

location /auth {
    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://keycloak;
}

```

注意:add_header Strict-Transport-Security ...部分很重要,并确保用户在使用https网址进入网站时将保持https协议。如果我错了,请纠正我!?

现在如果我访问以下网址:

https://my-example.com/auth/realms/jhipster/.well-known/openid-configuration

我会看到这个回应:

{
    "issuer": "http://my-example.com/auth/realms/jhipster",
    "authorization_endpoint": "http://my-example.com/auth/realms/jhipster/protocol/openid-connect/auth",
    "token_endpoint": "http://my-example.com/auth/realms/jhipster/protocol/openid-connect/token",
    "token_introspection_endpoint": "http://my-example.com/auth/realms/jhipster/protocol/openid-connect/token/introspect",
    "userinfo_endpoint": "http://my-example.com/auth/realms/jhipster/protocol/openid-connect/userinfo",
    "end_session_endpoint": "http://my-example.com/auth/realms/jhipster/protocol/openid-connect/logout",
    "jwks_uri": "http://my-example.com/auth/realms/jhipster/protocol/openid-connect/certs",
    "check_session_iframe": "http://my-example.com/auth/realms/jhipster/protocol/openid-connect/login-status-iframe.html",
    "grant_types_supported": ["authorization_code", "implicit", "refresh_token", "password", "client_credentials"],
    "response_types_supported": ["code", "none", "id_token", "token", "id_token token", "code id_token", "code token", "code id_token token"],
    "subject_types_supported": ["public", "pairwise"],
    "id_token_signing_alg_values_supported": ["RS256"],
    "userinfo_signing_alg_values_supported": ["RS256"],
    "request_object_signing_alg_values_supported": ["none", "RS256"],
    "response_modes_supported": ["query", "fragment", "form_post"],
    "registration_endpoint": "http://my-example.com/auth/realms/jhipster/clients-registrations/openid-connect",
    "token_endpoint_auth_methods_supported": ["private_key_jwt", "client_secret_basic", "client_secret_post"],
    "token_endpoint_auth_signing_alg_values_supported": ["RS256"],
    "claims_supported": ["sub", "iss", "auth_time", "name", "given_name", "family_name", "preferred_username", "email"],
    "claim_types_supported": ["normal"],
    "claims_parameter_supported": false,
    "scopes_supported": ["openid", "offline_access"],
    "request_parameter_supported": true,
    "request_uri_parameter_supported": true
}

您可能会注意到http://my-example.com/...而不是https://my-example.com/...

因此我不得不改变我的领域(jhipster-realm.json)的配置 "sslRequired" : "external","sslRequired" : "none",我不知道是不是坏事?考虑(1)当我测试登录工作流时,我的浏览器从不离开https;(2)我的keycloak实例可以通过任何公共端口访问。

好吧,我不会接受我自己的答案作为公认的答案,因为正如我之前所说,我觉得事情可以更好,更安全地完成。谢谢!

<强>更新

我使用https协议进行了以下更改:

Dockerfile

FROM jboss/keycloak:3.4.1.Final

standalone.xml

<server name="default-server">
    ...
    <http-listener name="default" socket-binding="http" redirect-socket="proxy-https" proxy-address-forwarding="${env.PROXY_ADDRESS_FORWARDING}" certificate-forwarding="true" enable-http2="true"/>
    <https-listener name="https" socket-binding="https" security-realm="ApplicationRealm" proxy-address-forwarding="${env.PROXY_ADDRESS_FORWARDING}" certificate-forwarding="true" enable-http2="true"/>
...

<socket-binding-group ...
    <socket-binding name="proxy-https" port="443"/>
...

nginx.conf

upstream keycloak {
    server keycloak:9443;
}
...
server {
    listen 80;
    return 301 https://$host$request_uri;
}
server {
    listen 443 ssl http2;
    ...
    location /auth {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass https://keycloak;
    }
...

jhipster-realm.json

...
"sslRequired": "external",

HTH,谢谢!