-1

我的应用程序 ( http://localhost:8081 )前面有一个 Keycloak Gatekeeper 代理 ( https://localhost:8080 ) 来卸载 OAuth2。它连接到 Keycloak 服务器 ( https://keycloak.my.company )。通过 Gatekeeper 登录和访问应用程序工作正常。

现在我有一个每 10 秒执行一次以重新加载某些内容的 AJAX 片段。这仅在超过 JWT 生命周期之前有效。然后 Gatekeeper 向 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

火狐告诉我:

Quellübergreifende (Cross-Origin) Anfrage blockiert: Die Gleiche-Quelle-Regel verbietet das Lesen der externen Ressource auf https://keycloak.my.company/auth/realms/demo/p …nid+email+profile&state=c758c30a-1c1c- 46f7-9155-f71b906ac61a。(Grund:CORS-Kopfzeile 'Access-Control-Allow-Origin' fehlt)。

(翻译:Blocked as Same-Origin-Policy forbids reading the resource. Reason CORS header 'Access-Control-Allow-Origin' is missing)

我想到的问题:

  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。使用 Redis,我现在遇到了https://issues.redhat.com/browse/KEYCLOAK-11077中描述的问题

4

1 回答 1

0

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

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

  2. 在 Realm Settings -> Tokens 下的 Keycloak 中,“SSO Session Idle”设置为与“Access Token Lifespan”相同的值,使其无用,因为它在需要刷新时也已经过期。

于 2020-01-22T08:31:13.190 回答