【发布时间】:2022-01-05 12:06:28
【问题描述】:
我已经构建了一个带有 angular 的 webapp 和一个带有 nestjs 的后端。作为身份验证,我想使用我们的 keycloak 服务器(jwt 令牌)。
我正在使用 OpenID Connect 机制。
在开发模式下,一切正常(前端:http://localhost:4200,后端:http://localhost:3030,keycloak(生产服务器):https://auth.域)
相应的调用包含以下标头:
Request URL: http://localhost:3030/v1/auth/keycloak/callback?session_state=*****
Request Method: GET
Status Code: 302 Found
Remote Address: [::1]:3030
Referrer Policy: strict-origin-when-cross-origin
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 88
Content-Type: text/html; charset=utf-8
Date: Wed, 05 Jan 2022 10:30:06 GMT
Keep-Alive: timeout=5
Location: http://localhost:4200/
Set-Cookie: Authorization=Bearer%20ey****; Path=/
Vary: Accept
X-Powered-By: Express
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
Connection: keep-alive
Host: localhost:3030
sec-ch-ua: "(Not(A:Brand";v="8", "Chromium";v="100", "Google Chrome";v="100"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: cross-site
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4758.9 Safari/537.36
但现在我尝试将后端部署到生产服务器。它在 nginx 代理管理器后面的 docker 容器中运行。所以调用是 angular(http://localhost:4200) -> nginx proxy(https://api.domain) -> nestjs(container:3030) -> keycloak(https://auth.domain)。
调用似乎成功了,后端日志告诉我它已成功接收到令牌。前端也没有显示任何错误,但是没有设置cookie,我不知道为什么。
检查网络调用,一切都应该没问题。 set-cookie 标头在那里,但只是没有设置,或者我无法从浏览器/js 访问它(因为我在浏览器开发工具中找不到它)。请注意,我的通话中没有“httpOnly”标志 - 这似乎是一个不同的问题。
这是与生产后端的调用:
Request URL: https://api.domain/v1/auth/keycloak/callback?session_state=****
Request Method: GET
Status Code: 302
Remote Address: 10.64.0.101:443
Referrer Policy: strict-origin-when-cross-origin
access-control-allow-credentials: true
access-control-allow-origin: *
content-length: 88
content-type: text/html; charset=utf-8
date: Wed, 05 Jan 2022 11:38:30 GMT
location: http://localhost:4200/
server: openresty
set-cookie: Authorization=Bearer%20ey***; Path=/
vary: Origin, Accept
x-powered-by: Express
x-served-by: api.mtg
:authority: api.mtg
:method: GET
:path: /v1/auth/keycloak/callback?session_state=***
:scheme: https
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
accept-encoding: gzip, deflate, br
accept-language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
cookie: Authorization=Bearer%20eyJhbG***
sec-ch-ua: "(Not(A:Brand";v="8", "Chromium";v="100", "Google Chrome";v="100"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
sec-fetch-dest: document
sec-fetch-mode: navigate
sec-fetch-site: cross-site
sec-fetch-user: ?1
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4758.9 Safari/537.36
我怀疑这是某种 cors 问题,但我无法理解它......
【问题讨论】:
-
您的设计似乎是“定制的”。为什么不使用标准授权代码流 + PKCE 作为前端(因此前端将直接与 OIDC 服务器一起进行身份验证,而无需任何后端)。后端只会验证请求中的令牌(但是为什么使用 cookie,当
Authorization标头是 API 请求身份验证的标准时?)。我会坚持使用更标准的方法,而不是在自己的自定义实现中破解它。 -
其实不是,它使用的是 OpenID Connect。我认为您有这种印象,因为我描述得很糟糕。没有生产模式和 https 代理它也可以正常工作。我能找到的所有潜在问题都不适用于我 - 没有设置 httpOnly,没有设置安全标志......尽管如此,cookie 仍然不会被设置
标签: angular cookies oauth-2.0 nestjs keycloak