将spring boot部署到tomcat时出现“未找到指定孩子的公钥”错误

Bad*_*uju 3 tomcat keycloak

我正在使用 Keycloak 来保护我的其余 api

我在 Keycloak 中有 2 个客户:

-我的前端:React 应用程序

-我的后端:Spring boot(仅承载类型)

当我运行 Spring Boot 应用程序时,一切正常。但是当我在tomcat中运行它时,出现错误401“没有找到指定孩子的公钥”。下面的一些配置:

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"
}
Run Code Online (Sandbox Code Playgroud)

网络.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>
Run Code Online (Sandbox Code Playgroud)

上下文.xml

<Context path="/myapp">
    <Valve className="org.keycloak.adapters.tomcat.KeycloakAuthenticatorValve"/>
</Context>
Run Code Online (Sandbox Code Playgroud)

keycloak-7.0 和 tomcat-8

你有什么主意吗?

Bad*_*uju 7

最后,我找到了解决方案。

\n\n

请注意 KC 管理仪表板中配置的默认签名算法令牌:

\n\n

在此输入图像描述

\n\n

HS256 需要 http://{domain}:8080/auth/realms/{realm-name}/.well-known/openid-configuration 中的公钥,不幸的是它不在那里。然后出现 \xe2\x80\x9cDidn\t find publicKey for指定孩子\xe2\x80\x9d 错误

\n\n

仅供参考:RS256 与 HS256:有什么区别?

\n\n

如果您使用 HS256,请确保 OIDC 元数据端点中的公钥

\n\n

使用其他算法

\n