Keycloak issuer validation and multi-tenancy approach

前端 未结 2 1653
长情又很酷
长情又很酷 2021-01-18 17:08

Let\'s say we have several micro-services. Each of them uses Keycloak authentication. We have also load balancer based on for ex. nginx which has external URLs and different

相关标签:
2条回答
  • 2021-01-18 17:21

    In case this helps anyone out during the early stages of development, you can set the Host header to the keycloak url that your backend service will use during the validation of the token. This way, the generated token will contain your Host header url in the issuer field. In my sandbox, I had keycloak running on docker at keycloack:8080 and a functional test calling keycloack via localhost:8095 to request a token (direct grant). Before setting the Host header to keycloack:8080, the issuer field was being set to localhost:8095 and the token was failing the validation with the "Invalid token issuer" error, since the backend service connects to keycloak on keycloak:8080 and TokenVerifier.java does the following check.

            public boolean test(JsonWebToken t) throws VerificationException {
                if (this.realmUrl == null) {
                    throw new VerificationException("Realm URL not set");
                } else if (!this.realmUrl.equals(t.getIssuer())) {
                    throw new VerificationException("Invalid token issuer. Expected '" + this.realmUrl + "', but was '" + t.getIssuer() + "'");
                } else {
                    return true;
                }
            }
    

    Reference: https://github.com/keycloak/keycloak-community/blob/master/design/hostname-default-provider.md

    0 讨论(0)
  • 2021-01-18 17:33

    Unfortunately Keycloak is too restrictive with its token validation according to the issuer ("iss") field in the token. It requires that the URL used to validate the token matches the URL in the "iss" field.

    A while ago I have opened a JIRA ticket for that problem (vote for it!): https://issues.jboss.org/browse/KEYCLOAK-5045

    0 讨论(0)
提交回复
热议问题