我正在使用Keycloak保护我的其余api
我在Keycloak中有2个客户端:
-我的前端:React应用
-我的后端:Spring Boot(仅承载型)
当我运行spring boot应用程序时,一切正常。但是,当我在tomcat中运行它时,出现了错误401“找不到指定孩子的publicKey”。以下是一些配置:
keycloak.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"
}
web.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>
context.xml
<Context path="/myapp">
<Valve className="org.keycloak.adapters.tomcat.KeycloakAuthenticatorValve"/>
</Context>
keycloak-7.0和tomcat-8
你有什么主意吗?
最后,我找到了解决方案。
请注意在KC管理控制台中配置的默认签名算法令牌:
HS256需要http:// {domain}:8080 / auth / realms / {realm-name} /.well-known/openid-configuration中的公钥,不幸的是,它不存在。然后出现“找不到指定孩子的公钥”错误
仅供参考:RS256与HS256:有什么区别?
如果使用HS256,请确保OIDC元数据终结点中的公钥
或使用其他算法