由于CORS,Keycloak Gatekeeper无法刷新令牌

时间:2020-01-21 10:47:28

标签: cors keycloak keycloak-gatekeeper

我在应用程序(https://localhost:8080)前面有一个Keycloak Gatekeeper代理(http://localhost:8081),用于卸载OAuth2。它与Keycloak服务器(https://keycloak.my.company)连接。通过Gatekeeper登录并访问该应用程序可以正常工作。

现在我有一个AJAX代码段,每10秒钟执行一次以重新加载某些内容。这仅在超过JWT生命周期之前有效。然后,网闸将重定向发送到Keycloak(应该这样做),但是由于CORS错误,AJAX请求无法跟随它。

请求看起来像这样:

OPTIONS https://keycloak.my.company/auth/realms/demo/protocol/openid-connect/auth
        ? client_id=demo-app-spring
        & redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Foauth%2Fcallback
        & response_type=code
        & scope=openid+email+profile
        & state=33a3da4d-1c83-4363-857c-511b26706649

    Host: keycloak.my.company
    User-Agent: ...
    Accept: */*
    Accept-Language: ...
    Accept-Encoding: gzip, deflate, br
    Access-Control-Request-Method: GET
    Access-Control-Request-Headers: x-requested-with
    Origin: http://localhost:8080
    DNT: 1
    Connection: keep-alive

响应为:

HTTP/1.1 200 OK
    Content-Length: 93
    Content-Type: application/json
    Date: Mon, 20 Jan 2020 14:20:33 GMT
    Set-Cookie: _0bc78=http://172.26.12.34:8080; Path=/
    Vary: Accept-Encoding

Firefox告诉我:

Quellübergreifende(Cross-Origin)Anfrage blockiert:Die Gleiche-Quelle-Regel Verbietet das Lesen der externen资源 https://keycloak.my.company/auth/realms/demo/p…nid + email + profile&state = c758c30a-1c1c-46f7-9155-f71b906ac61a。 (Grund:CORS-Kopfzeile的“访问控制-允许-来源”提示)。

(翻译:因为Same-Origin-Policy阻止,禁止读取资源。原因是缺少CORS标头“ Access-Control-Allow-Origin”)

我想到的问题:

  1. 由于X-Requested-With标头,为什么不将重定向计为“ CORS简单请求”?
  2. Keycloak为什么不回答CORS请求?它以“ +”作为CORS的来源,以“ https://localhost:8080/ *”作为有效的重定向URL
  3. 这毕竟是刷新令牌的正确方法吗?还是客户端(Javascript)检查生存时间并在实际请求之前在 之前刷新JWT的标准要求?

在此先感谢您的提示!

编辑: 配置:

verbose: true

listen: :8080
redirection-url: https://localhost:8080

tls-cert: local/localhost.crt
tls-private-key: local/localhost.key

upstream-url: http://localhost:8081/
skip-upstream-tls-verify: true

client-id: demo-app-spring
client-secret: xxx
discovery-url: https://keycloak.my.company/auth/realms/internal

secure-cookie: true

enable-logging: true

enable-refresh-tokens: true
encryption-key: xxx
store-url: redis://localhost:6379/

enable-default-deny: true
resources:
  - uri: /
    white-listed: true
   ...

enable-cors: true
cors-origins:
 - 'https://localhost:8080'
 ...
cors-methods:
 - GET
 - POST
 - OPTIONS
 - DELETE
 - PUT
cors-headers:
 - authorization
 - content-type
 - Cookie
 - authorization
 - content-type
 - accept
 - x-requested-with
 - origin
 - referer

我的第一次尝试没有重做。现在,借助Redis,我遇到了https://issues.redhat.com/browse/KEYCLOAK-11077

中描述的问题

1 个答案:

答案 0 :(得分:0)

要回答我自己的问题,有两个问题:

  1. Du https://issues.redhat.com/browse/KEYCLOAK-11077的Redis必须被禁用

  2. 在“密钥克隆”的“领域设置”->“令牌”下,“ SSO会话空闲”被设置为与“访问令牌寿命”相同的值,由于它在刷新时已经过期,因此也变得无效。需要。