Keycloak-gatekeeper: 'aud' claim and 'client_id' do not match

If, like me, you want to automate the keycloak config, you can use kcadm

/opt/jboss/keycloak/bin/kcadm.sh \
        create clients/d3170ee6-7778-413b-8f41-31479bdb2166/protocol-mappers/models -r your-realm \
        -s name=audience-mapping \
        -s protocol=openid-connect \
        -s protocolMapper=oidc-audience-mapper \
        -s config.\"included.client.audience\"="your-audience" \
        -s config.\"access.token.claim\"="true" \
        -s config.\"id.token.claim\"="false"

This is due to a bug: https://issues.jboss.org/browse/KEYCLOAK-8954

There are two workarounds described in the bug report, both of which appear to do basically the same thing as the accepted answer here but can be applied to the Client Scope role, so you don't have to apply them to every client individually.


With recent keycloak version 4.6.0 the client id is apparently no longer automatically added to the audience field 'aud' of the access token. Therefore even though the login succeeds the client rejects the user. To fix this you need to configure the audience for your clients (compare doc [2]).

Configure audience in Keycloak

  • Add realm or configure existing
  • Add client my-app or use existing
  • Goto to the newly added "Client Scopes" menu [1]
    • Add Client scope 'good-service'
    • Within the settings of the 'good-service' goto Mappers tab
      • Create Protocol Mapper 'my-app-audience'
        • Name: my-app-audience
        • Choose Mapper type: Audience
        • Included Client Audience: my-app
        • Add to access token: on
  • Configure client my-app in the "Clients" menu
    • Client Scopes tab in my-app settings
    • Add available client scopes "good-service" to assigned default client scopes

If you have more than one client repeat the steps for the other clients as well and add the good-service scope. The intention behind this is to isolate client access. The issued access token will only be valid for the intended audience. This is thoroughly described in Keycloak's documentation [1,2].

Links to recent master version of keycloak documentation:

  • [1] https://github.com/keycloak/keycloak-documentation/blob/master/server_admin/topics/clients/client-scopes.adoc
  • [2] https://github.com/keycloak/keycloak-documentation/blob/master/server_admin/topics/clients/oidc/audience.adoc

Links with git tag:

  • [1] https://github.com/keycloak/keycloak-documentation/blob/f490e1fba7445542c2db0b4202647330ddcdae53/server_admin/topics/clients/oidc/audience.adoc
  • [2] https://github.com/keycloak/keycloak-documentation/blob/5e340356e76a8ef917ef3bfc2e548915f527d093/server_admin/topics/clients/client-scopes.adoc