我正在使用 Keycloak 来保护我的 rest api
我在 Keycloak 中有 2 个客户端:
-我的前端:React 应用
-我的后端:Spring boot(bearer only type)
当我运行 spring boot 应用程序时,一切正常。 但我收到错误 401“没有找到指定 child 的公钥” 当我在 tomcat 中运行它时。 下面的一些配置:
key 斗篷.json
{
"realm": "pet",
"bearer-only": true,
"auth-server-url": "http://localhost:8080/auth",
"ssl-required": "none",
"resource": "secure-api",
"confidential-port": 0,
"enable-cors" : true,
"cors-max-age" : 1000,
"cors-allowed-methods" : "POST, PUT, DELETE, GET",
"cors-exposed-headers" : "WWW-Authenticate, My-custom-exposed-Header"
}
网络.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<module-name>myapp</module-name>
<security-constraint>
<web-resource-collection>
<web-resource-name>Authors</web-resource-name>
<url-pattern>/management/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>user</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>pet</realm-name>
</login-config>
<security-role>
<role-name>admin</role-name>
</security-role>
<security-role>
<role-name>user</role-name>
</security-role>
</web-app>
上下文.xml
<Context path="/myapp">
<Valve className="org.keycloak.adapters.tomcat.KeycloakAuthenticatorValve"/>
</Context>
keycloak-7.0 和 tomcat-8
你有什么想法吗?
最佳答案
最后,我找到了解决方案。
注意您在 KC 管理仪表板中配置的默认签名算法 token :
HS256 需要 http://{domain}:8080/auth/realms/{realm-name}/.well-known/openid-configuration 中的公钥,不幸的是它不在那里。然后出现“没有找到指定 child 的公钥”错误
仅供引用:RS256 vs HS256: What's the difference?
如果使用 HS256,请确保 OIDC 元数据端点中的公钥
或使用其他算法
关于tomcat - "Didn' t find publicKey for specified kid”部署spring boot到tomcat时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59981980/