Keycloak invalid_token 没有找到指定孩子的公钥

Keycloak invalid_token Didn't find publicKey for specified kid

我正在使用 keycloak 来保护我的休息服务并且我能够获得令牌,但是当我使用这个令牌来获得我的休息服务响应时,出现以下错误:

HTTP/1.1 401 Unauthorized
Cache-Control: no-cache, no-store, must-revalidate, private
X-Powered-By: Undertow/1
X-XSS-Protection: 1; mode=block
Server: WildFly/11
X-Frame-Options: SAMEORIGIN
Date: Wed, 30 Jan 2019 07:42:45 GMT
Connection: keep-alive
WWW-Authenticate: Bearer realm="demorealm", error="invalid_token", error_description="Didn't find publicKey for specified kid"
X-Content-Type-Options: nosniff
Content-Type: text/html;charset=UTF-8
Content-Length: 71
<html><head><title>Error</title></head><body>Unauthorized</body></html>

我点击了以下链接,但没有得到任何输出来解决这个问题。

keycloak bearer token error - Didn't find publicKey for specified kid

Didn't find publicKey for kid ,Keycloak?

编辑 1 -

根据详细信息,使用以下命令可以正确生成令牌

curl -X POST -k -H 'Content-Type: application/x-www-form-urlencoded' -i 'https://<IP-ADDRESS>/auth/realms/apirealm/protocol/openid-connect/token' --data 'username=cwlcadmin&password=password@123&client_id=api-client-id&grant_type=password&client_secret=a682049d-587c-4c38-a594-814f08b0ca76'a@123&client_id=api-client-id

但是当使用此令牌命中 rest-api 时,上述声明异常即将到来

curl -X GET -k -H 'Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJpeVlweXZtQU9Sc2RtNXlibWNqWUVSRUxJTnVFR2RNOThDeFVMSmdUTHFvIn0.eyJqdGkiOiJkZWQ4YzAzNC02NDM2LTRmNDAtYjZlNC0zYTI3MzcyNDJkODYiLCJleHAiOjE1NDg4NDUzMTQsIm5iZiI6MCwiaWF0IjoxNTQ4ODQzNTE0LCJpc3MiOiJodHRwczovLzEwLjUzLjIwMS4yMDcvYXV0aC9yZWFsbXMvYXBpcmVhbG0iLCJhdWQiOiJhcGktY2xpZW50LWlkIiwic3ViIjoiZjo4OWYzMDE0MC1k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VPB8vsUy55IcrZGNJWVFsWcvudqTXwW2Tg8BBq-tsFkVuABs5w' -H 'Content-Type: application/json' -i 'https://<IP-ADDRESS>/api/v2/0/zones'

仅供参考 - 使用 Wildfly 和 Load-balancer 也由 Wildfly

提供

经过几天的努力,我们终于解决了Wildfly服务器中添加的子系统的问题,应该是这样的

<subsystem xmlns="urn:jboss:domain:keycloak:1.1">
            <secure-deployment name="sure-admin-web.war">
                <realm>Realm_Name</realm>
                <resource>CLIENT_APP</resource>
                <use-resource-role-mappings>true</use-resource-role-mappings>
                <auth-server-url>https://<KEYCLOAK-IP>:8666/auth/</auth-server-url>
                <ssl-required>NONE</ssl-required>
                <credential name="secret">7df18c0d-d4c7-47b1-b959-af972684dab0</credential>
            </secure-deployment>
        </subsystem>

在我们的例子中我们错过了 <ssl-required>NONE</ssl-required> 并且在 <auth-server-url> 中我们添加了错误的负载均衡器 url 而它应该是 Keycloak URL.

仅供参考 - 负载均衡器在 HTTPS 上工作,而 keycloak 运行 在 http

上工作

我有类似的问题。我的后端应用程序处于 spring 启动状态。 结果 application.properties 需要设置 keycloak.realm-key。

参考

https://github.com/keycloak/keycloak-documentation/blob/master/securing_apps/topics/oidc/java/java-adapter-config.adoc