尝试访问部署在 K8s/Argocd 服务器上的 Spring 后端时出现 "required audience is missing"

Getting a "required audience is missing" when trying to access my Spring backend deployed on a K8s/Argocd server

我有一个 Java Spring 后端,我已经使用 Kubernetes 清单部署到 ArgoCD 上。通过让用户登录到 Keycloak 来保护后端。部署后,我转到 /api/application-info,然后它让我登录到 Keycloak。但是在我登录后,它会将我重定向回来,我只看到 This page isn’t working. If the problem continues, contact the site owner. HTTP ERROR 401.

查看后端日志,我看到 Bearer error="invalid_token", error_description="An error occurred while attempting to decode the Jwt: The required audience is missing", error_uri="https://tools.ietf.org/html/rfc6750#section-3.1"。谷歌搜索后,一页说可能是客户端 ID 错误,但我仔细检查了一下,没问题。我在“application.yml”文件中设置 Keycloak 客户端 ID,例如:

spring:
  security:
    oauth2:
      client:
        provider:
          oidc:
            issuer-uri: https://mykeycloak.com/auth/realms/myrealm
        registration:
          oidc:
            client-id: myclientid
            client-secret: myclientpw

有人知道我还能如何调试吗?

这是虚拟服务。我知道它是有效的,因为 (1) 我可以去 /api,它会把我带到 /api/,(2) 我去 /api/application-info,然后它使我登录到 Keycloak。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ingress
spec:
  hosts:
    - myhost.io
  gateways:
    - public-gateway.istio-system.svc.cluster.local
  http:
    - match:
        - uri:
            exact: /api
      redirect:
        uri: /api/
    - match:
        - uri:
            prefix: /api/
      rewrite:
        uri: /api/
      route:
        - destination:
            host: mysite-be
            port:
              number: 80
      websocketUpgrade: true
    - match:
        - uri:
            prefix: /
      route:
        - destination:
            host: mysite-fe
            port:
              number: 80

JWT 令牌可以有一个可选的“aud”属性,它指示令牌的目标受众。您场景中的观众是您的 Spring 启动应用程序,这意味着应该针对访问您的 Spring 启动应用程序颁发令牌。

作为 Spring 启动应用程序上 JWT 令牌验证的一部分,如果“aud”属性 存在,它的值也会被检查以确保令牌被颁发应用程序使用。

我想为您的登录生成的令牌不包含“aud”属性 的 proper/expected 值。在这种情况下,您可能需要在 Keycloak 上配置您的客户端。

您可以通过将令牌粘贴到 https://jwt.io/ 并检查“aud”属性.

来查看令牌中的属性

解决此问题的解决方法 也可能是在您的应用程序中完全禁用受众验证。您可以通过将以下 属性 添加到您的 keycloak.json 文件来执行此操作:

{
    ...
    verify-token-audience: false
}

实际的解决方案应该是研究它的生成方式并使其产生适当的价值。我建议查看 Keycloak 文档中的 Audience Support,解释它们是如何生成的以及如何在 Keycloak 中进行配置。

基于 RFC-7519 JSON Web Token 规范:

The "aud" (audience) claim identifies the recipients that the JWT is intended for. Each principal intended to process the JWT MUST identify itself with a value in the audience claim. If the principal processing the claim does not identify itself with a value in the "aud" claim when this claim is present, then the JWT MUST be rejected. In the general case, the "aud" value is an array of case- sensitive strings, each containing a StringOrURI value. In the special case when the JWT has one audience, the "aud" value MAY be a single case-sensitive string containing a StringOrURI value. The interpretation of audience values is generally application specific. Use of this claim is OPTIONAL.

根据您描述的错误,可能是您的集群注入了额外的授权配置。您可能想要检查这些并确保您没有得到未被处理的 extra/unexpected 配置。