Repository navigation
Keycloak Behind Reverse Proxy and Internal Backend Access: “Invalid Bearer Token” Error #41251
Unanswered
roman-galyaminskiy
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
We use Keycloak 26.0.5 with a reverse proxy and have the following setup to support multiple tenants:
Reference: https://www.keycloak.org/server/reverseproxy#_relevant_options
https://tenantN.mydomain.com/api/keycloak/realms/myrealm/.well-known/openid-configurationreturns tenant-specific endpoints as expected:{ "issuer": "https://tenantN.mydomain.com/api/keycloak/realms/myrealm", "authorization_endpoint": "https://tenantN.mydomain.com/api/keycloak/realms/myrealm/protocol/openid-connect/auth", "token_endpoint": "https://tenantN.mydomain.com/api/keycloak/realms/myrealm/protocol/openid-connect/token", "introspection_endpoint": "https://tenantN.mydomain.com/api/keycloak/realms/myrealm/protocol/openid-connect/token/introspect", "userinfo_endpoint": "https://tenantN.mydomain.com/api/keycloak/realms/myrealm/protocol/openid-connect/userinfo", "end_session_endpoint": "https://tenantN.mydomain.com/api/keycloak/realms/myrealm/protocol/openid-connect/logout", ... }However, we are also using Keycloak for authorization, utilizing the Quarkus Keycloak authorization client. Backend services communicate with Keycloak using an internal URL.
Setting:
results in the following error from the backend:
And a corresponding warning in Keycloak logs:
The only way I managed to make it work was to use tenant-specific public URLs for the authorization client instead of the local one.
The "Invalid bearer token" message is not very informative. However, if I understand https://www.keycloak.org/server/hostname#_utilizing_an_internal_url_for_communication_among_clients correctly, it occurs because Keycloak rejects requests via internal URLs when
KC_HOSTNAME_BACKCHANNEL_DYNAMICis set to false. But I can't set it to true, because that requires specifying a static URL viaKC_HOSTNAME, which then disables dynamic hostname resolution fromX-Forwarded-*headers.My questions:
Is there a way to enable dynamic hostnames (via
X-Forwarded-*headers) and allow the backend to access Keycloak using internal URLs at the same time?Is the described setup a valid approach for multi-tenant deployments?
All reactions