Keycloak redirect URI and audience restrictions

Keycloak redirect URIs, web origins, client scopes and token audiences decide where tokens can go and what they contain. This page explains how to narrow each one for every client and how to verify the result.

On this page

Why Keycloak redirect URI rules decide who receives a token

A loose redirect URI or web origin can send an authorization code or token to the wrong place, even when users and passwords are correct. A Keycloak realm usually serves many clients, and each client carries its own list of valid redirect URIs, web origins, and scopes. Teams that copy a client configuration from a test environment, or add a wildcard to make a login work quickly, often leave those lists wider than any application needs. The result is that Keycloak will complete a login and hand the response to an address that a sign-in flow should never reach.

Each setting controls a different part of that exposure. Valid redirect URIs limit where Keycloak returns the authorization code or token after login. Web origins govern which browser origins may make cross-origin requests to Keycloak's token and userinfo endpoints. Default and optional client scopes decide which roles, groups and claims enter the token by default. The audience claim (aud) tells each API which services a token is meant for. Teams need to tighten these when a client is moved to production, when a single-page app or mobile app is added, when a realm starts serving more applications, or when a security review finds a client with a wildcard or an unused scope. Before changing anything, confirm which exact URLs each application uses and which APIs actually validate the token.

This page does not cover client authentication methods such as client secrets, signed JWTs or mutual TLS, and it does not cover how long tokens and sessions live. Those topics have their own pages in this group. It also does not cover how to lock down the admin console or how to enforce client-wide rules through client policies. Read this section as the scope of the redirect and audience work, and use the sibling topics for the rest of client hardening.

Which client settings to restrict, and when each fits

This table compares the Keycloak client settings that decide where tokens can go and who receives them. Read each row for what the setting controls, the restrictive value to start from, and the flexibility cost of tightening it. The main takeaway is that exact values per client are safer than patterns, and flexibility should be granted per environment rather than per realm.

SettingWhat it controlsRestrictive setting to preferTrade-off for flexible applications
Valid redirect URIsWhere Keycloak sends the authorization code or tokens after login. Any value that matches is accepted.Exact HTTPS callback URLs per client, with no wildcards and no bare path prefixes.Preview or review environments need their own exact entries. Teams often add wildcards instead, which widens the target for a stolen code.
Valid post logout redirect URIsWhere the browser may be sent after logout. Kept separate from login redirects in the client settings.Exact landing pages that the application actually uses after sign-out.Applications with many logout pages must list each one. Missing entries cause logout errors that users may work around by widening the list.
Web originsWhich browser origins may make cross-origin requests to Keycloak, which controls CORS responses for the token endpoint in browser apps.The exact scheme, host and port of each front end, or none for confidential server-side clients.Single-page apps served from several hosts need one entry each. Using a wildcard origin lets any site that obtains a token call Keycloak from a browser.
Default client scopesScopes and their mappers included in every token request without the client asking for them, including role and group mappers.Only identity scopes such as openid and profile, with role mappers moved to optional scopes or removed for that client.Removing a default scope can break claims the application already reads. Confirm which claims each application uses before changing it.
Optional client scopesScopes a client may request by name, which add their mappers only when requested.Narrow optional scopes that map to specific roles or audiences, requested only by the apps that need them.Each extra optional scope adds a request-time choice that applications and reviewers must understand. Document which client requests which scope.
Full scope allowedWhen enabled, every realm role and client role of the user is placed in the access token, regardless of the scope the client asks for.Disabled, with explicit scope mappings that add only the roles each client needs.Turning it off requires mapping every role the application checks. Missing mappings show up as authorization failures during testing.
Audience mappersAdds the aud claim so resource servers can confirm the token is meant for them.One audience per API, added through an audience mapper only to clients that call that API.Services that call several APIs need several audience mappers, and token size grows. Confirm each resource server validates the aud claim.
Consent and client-level accessWhether users see consent prompts and which users or roles may sign in to the client.Consent required for third-party clients, and role-based access on the client for internal apps.Consent prompts add friction for internal users. Use them selectively and confirm the behavior with each application owner.

Where Keycloak checks the redirect URI during a login

The flow starts when the browser sends the user to Keycloak's authorization endpoint with a redirect_uri. Keycloak compares that value with the redirect URIs registered for the client and rejects the request when nothing matches, so the first check is against the client's list. The code returns to the client application, which exchanges it at the token endpoint. For single-page apps that call the token endpoint from the browser, the web origins setting decides whether that cross-origin call is allowed. The aud claim is set in the issued access token, and the resource API checks it before serving data.

Keycloak and the resource API check redirect URI, web origin and audience at distinct points.

Five OAuth misconfigurations Keycloak clients commonly carry

Keycloak client settings decide which redirects, origins and tokens an application can receive. The five misconfigurations below look harmless in a quick review, yet each one lets a token or authorization code reach a party the client never intended. Check each one against the clients in your realm.

Wildcard redirect URIs

A redirect URI such as ` matches every path under the host, including pages that echo parameters or load third-party content. Wildcards on the host or in the middle of a URI match even more. Review each confidential and public client and list the exact callback URLs it uses. Remove any wildcard that covers paths the application does not serve, and confirm that the callback path is the only one the client can receive codes on.

Keycloak docs

Open redirectors in the application

An application that accepts a redirect_uri or next parameter and forwards the browser to it can turn a valid, exact Keycloak redirect URI into a bounce point. An attacker starts a login with a legitimate client, then sends the user through the application's own redirect to a page they control. Exact matching in Keycloak does not stop this. Test every post-login and post-logout redirect in the application and allow only a fixed set of internal destinations.

Web origins set to a wildcard

Web origins control which domains receive CORS headers from Keycloak's token endpoints. A value of + or *, or a long list of unrelated domains, lets any site that holds a token or cookie read Keycloak responses from a browser. Set each public client to the exact origin of its single-page app, and keep the list short. Confirm that the origin matches the scheme, host and port the browser actually uses.

Default scopes exposing roles

Client scopes assigned by default add their mappers to every token the client requests. When a default scope carries realm or client role mappers, each token lists roles the application may not need, and any service that accepts the token can read them. Move role and group mappers into optional scopes that a client requests explicitly, and keep the default scope limited to identity claims. Compare the token from each client with the roles that its code actually checks.

One audience for every service

When all clients share one audience value, or the default aud mapper adds every client to the token, an access token issued for a reporting API also works against a payments API. Map each API to its own audience with an audience mapper on the client that calls it, and grant only the clients that need that API. Check that services validate the aud claim against their own identifier, because a token without a valid audience should be rejected, not accepted.

How audience restrictions limit where a token can be used

The aud claim tells a resource server which API a token was issued for. Keycloak does not add a useful audience to every access token by default for every client setup, so the audience has to be designed on purpose. In Keycloak, audience values are typically added through client scopes that carry an audience mapper, which places a named client or resource identifier into the token's aud claim. The practical decision is to map each API to its own audience value, then attach that mapper only to the client scopes used by the applications that call that API. A web front end that only needs to identify the user should not receive a token that names the payments API, and the payments API should not accept tokens that do not name it.

Build the mapping from the inventory outward. List each resource server, decide which clients are allowed to request tokens for it, and create one audience per resource server rather than a shared value that every service accepts. Then check the result by decoding a token from each client and confirming that its aud values match what the design says. Use the Keycloak server administration guide for the release you run to confirm how audience mappers, the Included Client Audience setting and the Add to access token option behave, because details of claim placement have changed between releases and should not be assumed from older examples.

The restriction only works if the resource server enforces it. Each API should reject any token whose aud does not contain its own identifier, check the issuer and signature against the realm's keys, and refuse tokens that were issued for another client. A service that accepts any valid token from the realm turns audience restrictions into documentation rather than control. Ask the team that owns each API to show the validation code or the library configuration that performs this check, and test it with a token issued for a different audience to confirm it is refused.

How to tighten redirect URIs and web origins on an existing client

This sequence takes an existing Keycloak realm from broad, wildcard-based client settings to exact redirect URIs, minimal web origins, and token audiences limited to the APIs each client calls. Each change is tested in a non-production realm before it reaches production.

  1. Inventory every client and its registered URIs

    The realm administrator lists every client in the realm, including clients that were created for testing and no longer have an owner. For each client, record its access type, enabled flows, valid redirect URIs, valid post logout redirect URIs, web origins, and the application that uses it. Mark any client whose owning team cannot be identified. The inventory is complete when every client has an owner and a documented purpose, and every unowned client has a decision to keep or disable.

  2. Export the current client configuration

    Before changing anything, the administrator exports the realm configuration with the export tooling that Keycloak provides and stores the file under change control. The export is the rollback reference if a login breaks after a change. The administrator confirms the export contains the client definitions, protocol mappers, and client scopes, and that secrets are handled as the team's secret policy requires. The step closes when the export file is stored and its location is recorded in the change ticket.

  3. Replace wildcards with exact URLs per environment

    For each client, the team replaces wildcard entries in valid redirect URIs and web origins with the exact callback URLs the application uses in that environment. Development, staging, and production hosts each get their own entries, so a staging URI never sits in the production client. Application owners confirm each URL against their framework configuration. The check is a diff of the export against the new values, reviewed by the application owner, showing no remaining wildcard entries that match more than one host.

  4. Remove unused and overly broad web origins

    The team deletes web origins that no longer correspond to a running application, and removes origins that cover more hosts than the application serves. Redirect URIs that point to paths the application no longer handles are also removed. Each removal is logged with the owner's confirmation. The step is complete when each remaining origin maps to a named application and a named environment, and a sample of removed entries has been confirmed unused in access logs.

  5. Move role and group mappers out of default scopes

    The administrator reviews the default client scopes assigned to each client and identifies role and group mappers that place roles into every token. These mappers move into optional client scopes, and each client requests only the scopes it needs in its authorization request. Application owners confirm which roles their code reads from the token. The check is a decoded access token from a test login for each client, confirming it contains only the roles that application uses.

  6. Add audience mappers for each resource API

    For each API that accepts tokens, the team creates a client scope with an audience mapper that adds that API's client identifier to the token audience, and assigns it as optional to the clients that call the API. Tokens intended for one service then no longer carry audiences for unrelated services. The API owner verifies that the aud claim is checked in the token validation code. The check is a decoded token from each client showing only the audiences that client is entitled to call.

  7. Test each change in a non-production realm

    The team applies the changes first to a copy of the realm, either by importing the export into a separate realm or a separate environment. Test logins cover the expected flows, and the team also attempts logins using a redirect URI that is not on the list, which must fail with an invalid redirect error. Requests from an origin not in the web origins list must be refused for cross-origin calls. The step closes when every intended login passes, every unlisted redirect URI and origin is refused, and the results are recorded in the change ticket.

  8. Apply to production and retest the negative cases

    After the non-production results are approved, the team applies the same changes to production during a window agreed with application owners. Immediately after the change, the team repeats the positive login tests and the negative tests with a non-listed redirect URI. Owners confirm their applications can sign in and that the decoded tokens match the expected roles and audiences. The step is complete when the production results match the non-production results and the export, change record, and test evidence are stored together.

Restriction choices for single-page apps, web apps and APIs

A single-page application runs its code in the browser and is a public client, so it holds no client secret. Its security depends on exact redirect URIs, a tightly limited set of web origins and the Authorization Code flow with PKCE. Keycloak uses the redirect URI to decide where to send the authorization code, and a loose pattern lets an attacker-controlled page receive it. Web origins control which browser origins may make cross-origin requests to Keycloak endpoints, so they should list only the application's own origin.

  • Exact redirect URIs: Register the full callback address, such as the app's /callback path, and avoid trailing wildcards like `
  • Web origins limited to the app: Set the web origin to the application's own scheme, host and port, not + or * unless the team has confirmed a specific need.
  • PKCE required: Enable the PKCE code challenge method for the client so a stolen authorization code cannot be redeemed alone.
  • No implicit flow: Turn off the implicit flow and direct password grants for the client, since neither suits a browser-held token.
  • Scoped default scopes: Remove client scopes that add roles or attributes the page does not display or use.

Confirm with the front-end team which origins the application calls and whether any silent renewal iframe needs its own redirect URI.

Verifying restrictions and stopping drift over time

Restrictions on redirect URIs, web origins and audiences are only useful if you can show they work, and the proof should be a test anyone on the team can rerun. Start with negative login tests: attempt a sign-in with a redirect URI that is not registered on the client, a redirect URI that differs from a registered one only by path, port or a trailing segment, and an origin header from a domain that is not in the web origins list. Each attempt should fail at Keycloak with an invalid redirect URI or origin error rather than returning a code to the attacker-controlled location. Run the same tests against a client that uses a wildcard, so the risk is visible, and record the request, the response and the client settings used. Confirm the expected error behavior in your own Keycloak version, because error presentation differs between releases.

Positive tests matter as much as negative ones. Obtain tokens for each client and decode them to check the aud claim lists only the resource servers that client should call, and the scope claim contains only the client scopes you intended. Compare the roles in realm_access and resource_access against what the application needs. A token that carries an unexpected audience or a role from a default scope is a finding even if the login succeeded. Keep a short script that performs these checks for a fixed set of reference clients, and run it after every realm change.

Restrictions usually erode at three points: new clients, copied clients and environment changes. A developer who clones a working client to start a new application inherits its wildcards and default scopes, and a promoted configuration may carry development hostnames into production. The defense is to keep the exported realm or client configuration in version control, so that a change to a redirect URI, web origin or client scope appears as a diff that a second person must approve. Add a check that fails on wildcard redirect URIs and on origins that do not match an allowlist. For internal audit, keep the exported client definitions with their review history, the negative and positive test results for each release, the date of the last review of each client, and the approval record for every exception. Confirm with your auditors which format of evidence they accept.

Limits and what to confirm in your environment

The settings in this topic only close risk when the applications, libraries and Keycloak release around them cooperate. Treat each card below as a question to answer with the platform and application teams before you sign off on a client configuration.

Do frameworks need wildcard redirects?

Some frameworks and preview environments generate redirect URIs on the fly, which pushes teams toward wildcards such as ` Ask which hostnames and paths must be accepted, whether the framework can register a fixed callback per environment, and whether a short, explicit list of preview hosts can replace the pattern. Every wildcard widens the set of hosts that can receive authorization codes, so each wildcard needs a named owner and an expiry date.

Keycloak docs

Does any legacy client need implicit flow?

Older single-page apps and internal tools may still rely on the implicit flow or on broad web origins that were added to make a browser call succeed. Ask which clients have implicit flow or direct access grants enabled, which ones send tokens through the browser, and which web origins are used only for CORS. Clients that cannot move to the authorization code flow with PKCE should be listed with a dated remediation plan, and web origins should match the exact scheme, host and port the app uses.

Do resource servers check the aud claim?

Restricting the audience in Keycloak only helps if each API rejects tokens that were not issued for it. Ask each service owner whether their middleware validates aud, iss and the signature, and whether it accepts tokens minted for a different client. Shared client scopes that add several audiences should be reviewed, because a token that carries many audiences is usable against all of them. Test with a token meant for another service and confirm it is refused.

How does your Keycloak release handle these settings?

Confirm which Keycloak release you run and whether your custom providers, themes or authenticators change how redirect URIs, web origins or audience mappers are evaluated. Check the release notes for your version before upgrading, since security-related defaults can change between releases. Ask the vendor or your own test team to confirm the behavior in a staging realm, because a setting that passes in one release can behave differently after an upgrade.

Current release
26.8.1
Upgrading guide

Questions teams ask before tightening Keycloak client restrictions

How much effort does a Keycloak client review take?

The effort depends mostly on how many clients the realm holds, how many environments each client runs in, and how many applications share one client. Each client needs its redirect URIs, web origins, default and optional scopes, and audience mappers checked against what the application actually calls. A shared client used by several applications takes longer because every consumer must be tested before a restriction lands. Testing depth also varies: a public single-page app with a simple login flow needs less verification than a backend service that exchanges tokens with other services. Ask any partner to break the quote down by client count, environment count and test depth so you can see which factor drives the price.

Can our own team tighten Keycloak client settings without outside help?

Yes, a small realm with clear owners can usually handle this internally, provided one person can read the inventory, one person approves changes, and each application owner can confirm their login flows. The work becomes harder when many teams own clients, when clients are shared, or when no one remembers why a wildcard redirect URI was added. In those cases the main cost is coordination, not configuration. Start by exporting the realm, listing every client with its owner, and marking the clients nobody can explain. That inventory tells you whether an internal team can finish the work or whether an outside review will save time.

How long does it take to tighten redirect URIs and audiences?

Duration depends on inventory size, the number of applications that depend on each client, and the change windows your organization allows for authentication changes. A realm with a short client list and a standard change process moves much faster than one where every change needs a maintenance window and sign-off from several owners. Testing against a non-production realm also adds time because each application's login, logout and token refresh paths need to be exercised. There is no fixed duration that applies across environments, so ask for a timeline in the written scope that lists the client inventory, the test cycles and the change dates before work starts.

What does the organization need to provide for this work?

The work needs four things from your side: application owners who can confirm the exact redirect URIs, web origins and API audiences each app uses; a test realm or staging Keycloak that mirrors production clients; a change approval path that covers authentication changes; and administrator access to the admin console or to the configuration export and import process. Owners matter most, because a restriction that looks unused may be needed by a rarely used callback, a mobile deep link or a scheduled job. Confirm access and approvals before scheduling, since waiting for them is one of the most common causes of delay.

When should we hire a partner for Keycloak client restrictions?

Consider a partner when the realm is large, when clients are shared across business units, or when the configuration must hold up under an audit. In those situations the risk is not the individual change but the lack of an inventory, unclear ownership and missing evidence of what was changed and tested. DeployOpen implements and hardens self-hosted platforms inside your own infrastructure and prepares the evidence an assessor may ask for, but it does not issue audit reports or certifications. Independent auditors and certification bodies make those determinations. A partner can also run the work against a written scope, which makes the boundaries of the change clear before any client is touched.

What happens if a tighter redirect URI breaks a login?

Roll back first, then fix the exact URI. Keep an exported copy of the realm or client configuration taken before each change, so the previous redirect URIs, web origins and scopes can be restored quickly. Once users can sign in again, compare the URI the application sent in its authorization request with the value registered in the client, including scheme, host, port, trailing slash and path. Correct the single entry that did not match rather than reopening a wildcard, and retest the login, logout and token refresh paths. Record the failure and the fix so the same gap does not return during the next review.

How DeployOpen scopes and delivers client restriction work

DeployOpen plans, implements, tests and documents Keycloak redirect URI, web origin and audience restrictions inside your own infrastructure, as part of a self-hosted Keycloak deployment. Each client is reviewed against the applications it serves, then tightened and verified before handover.

How we scope

  • Every client inventory covers redirect URIs, web origins, default and optional scopes, and the APIs each application calls.
  • The written scope names the realms, clients and environments in which redirect and audience rules will be changed.
  • Engagements are fixed-scope, with deliverables and timelines agreed in writing before any client configuration changes start.
  • Scope also states which applications need owner sign-off, since a tightened redirect URI can break a sign-in flow.

How we build

  • Exact redirect URIs replace wildcards, with a list of each registered callback and the application that owns it.
  • Web origins are limited to the hosts that run each front end, and default client scopes are reduced to the roles each client needs.
  • Audience mappers are set so each access token names only the APIs that will accept it, with the mapper configuration recorded.
  • Changes are made through configuration as code or scripted admin calls where the client's configuration needs to be repeatable.

How we hand over

  • Deliverables include a client-by-client register of redirect URIs, web origins, scopes and token audiences, kept current with each change.
  • Test evidence shows that unregistered redirect URIs, foreign origins and unexpected audiences are rejected by Keycloak.
  • Runbooks explain how to add a redirect URI or audience through review, so the restrictions survive later changes.
  • Handover includes a walkthrough with the client owners and the platform team who will maintain the register.

How we support

  • Ongoing support, upgrades and continuous testing are available as retainers after handover.
  • A retainer can cover periodic review of new clients against the redirect and audience standard set during the engagement.
  • Retesting follows Keycloak upgrades or changes to client policies, so restrictions are checked again after each change.
  • Ask for the retainer terms and review cadence in the quote, since both determine how new clients are onboarded.

Sources and further reading

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.