Writing custom Keycloak required actions

Custom Keycloak required actions force users through a step after login, such as accepting updated terms or confirming profile data. This page covers the required action SPI, enabling and triggering actions, and the upgrade cost of owning the code.

On this page

Why teams write custom Keycloak required actions

Teams write a custom required action when every user must complete a step after signing in, such as accepting updated terms of use, confirming profile data or finishing a one-time check, and none of Keycloak's built-in required actions cover it. In Keycloak, a required action is attached to a user and enforced during login before the user receives tokens. A custom action is built with the required action SPI, which means a provider class and a factory that Keycloak loads at startup, a form the user completes, and a registration that makes the action selectable in the admin console. This page covers that build, enabling the action in a realm, triggering it for individual users, and sending applications the ability to request it on demand.

The usual alternative is to put the step inside each application. That works for one or two apps, but it fragments the policy: every application must store the acceptance, every team must update its code when the terms change, and a user who signs in to a new app may see a different flow than one who signs in to the old app. A required action keeps the step in one place, so the same acceptance applies to every client in the realm. Teams should still keep application-specific logic in the application, and use the required action only for steps that belong to the identity layer, such as recording consent or verifying profile data before any client receives a token.

A custom required action is an extension, so it carries upgrade cost that a configuration change does not. The SPI and the templates it uses can change between Keycloak releases, so each upgrade needs a compile and test pass against the target version. Before committing, confirm with your team which Keycloak version you will run, check the upgrade notes for the SPI and form templates, and decide who owns the code after launch. The cost of building the action is one-time, but the cost of keeping it compatible with each release recurs for as long as the action stays in production.

What a required action does in Keycloak

A required action is a step Keycloak runs for a user after credentials are checked. The cards cover the behavior the server administration guide describes, the parts you configure in the admin console, and the provider you write. Confirm each detail against the Keycloak version you plan to run before you build.

Runs after authentication

A required action runs after Keycloak checks the user's credentials and before the login completes. Keycloak presents the step's own form or screen, then continues the flow. Because the step sits in the login path, a change to it reaches sign-in for every affected user. Test it inside the same flows your users follow, and check how it interacts with existing actions such as terms acceptance and password updates.

Blocks token issuance

Until the user completes a pending required action, Keycloak does not issue the tokens that would let the application sign them in. This is the property that makes the step useful for terms acceptance or profile confirmation, since the application cannot skip it by calling the token endpoint directly. Confirm how the target version handles sessions started before the action was assigned.

Server admin guide

Assigned to users or requested by clients

An administrator can assign a required action to an individual user in the admin console, which suits one-off prompts. A client can also request an action during an authorization request, so the application decides when the step appears. Decide which trigger owns each requirement before you build, because mixing user assignments and client requests for the same step makes it hard to see why a user was prompted. Confirm the exact request parameter, such as kc_action, in the target version's docs.

Implemented as a provider

A custom required action is a provider on the required action SPI: a factory that Keycloak loads from a deployed JAR, plus a provider class that renders the form and evaluates the response. Once deployed, the action appears in the realm's required action list. Keep the decision logic that determines whether the user passes in your own service code, so a change to the form does not change the security outcome.

Enabled before it is assigned

A required action must be registered and enabled in the realm before it can be assigned to a user or requested by a client. Custom actions sit in the same list as built-in actions such as password update and terms acceptance. Check the enabled and default settings after each deployment, because a provider can load correctly and still be unavailable to users until a realm administrator turns it on.

Upgrade exposure

Custom providers compile against Keycloak's SPI interfaces, so an upgrade can break the build or change runtime behavior even when the form still looks correct. The project says breaking changes in a minor release will be opt-in, which narrows the risk without removing it. Rebuild and retest the provider against each target release, and read the upgrade notes for the required action interfaces before you schedule a change.

Current release
26.8.1
Upgrading guide

Which approach fits which requirement

This table compares four ways to add a step after or during sign-in: a built-in required action, a custom required action, a custom authenticator in a flow, and a page in the application itself. Read each row to see what triggers the step, who decides when it runs, and how exposed it is to Keycloak upgrades. The main takeaway is that a step every login must pass fits a flow or required action in Keycloak, while a one-time request from one application usually fits the application.

ApproachTrigger and who controls the stepUpgrade exposure and when it fits
Built-in required actionSet per user or added by an administrator in the console. Keycloak runs the step, and the admin chooses which users receive it.Lowest exposure because it ships with Keycloak. Fits standard steps such as updating a password or configuring OTP.
Custom required actionAssigned per user by an admin, a script, or the Admin REST API, or set as a default action for new users. Your provider code runs the step.Moderate exposure because your provider depends on Keycloak SPI interfaces. Fits a one-time step such as accepting revised terms or confirming profile data.
Custom authenticator in a flowRuns automatically for every login that reaches its position in the authentication flow, based on conditions you set. Keycloak's flow engine controls when it runs.Higher exposure because it sits in the sign-in path and must be tested with each upgrade. Fits checks that must apply to every login, such as a risk or step-up check.
Application-side pageTriggered by the application after Keycloak returns a token, for example when a claim is missing. The application controls when and whether the step runs.No Keycloak code to maintain, but the check lives outside the identity layer. Fits data requests scoped to one application that other applications do not need.
Terms acceptance for every loginUsually a custom authenticator or a flow step, so it runs on each sign-in until the user accepts the current version.Use a flow when acceptance must be enforced across every application in the realm, and confirm how your auditors expect acceptance to be recorded.
One-time profile data checkA custom required action assigned to affected users, or a required action that an application requests through the OIDC kc_action parameter.Required actions suit a one-time check. Confirm the kc_action behavior in your Keycloak version before relying on it.
Who maintains the codeYour team owns custom providers. DeployOpen or another partner can build them, but the upgrade cost stays with the owner of the code.Ask for a written list of each custom provider, the Keycloak version it was tested on, and the test cases used for the next upgrade.

How to build and deploy a required action

This sequence turns a post-login requirement into a tested Keycloak provider, enabled in the realm and triggered reliably for individual users and from applications. Each step ends with an artifact your team can review before the next one starts.

  1. Define the requirement and completion condition

    Write down what the user must do, which users and clients the step applies to, and the exact condition that marks the action complete, such as a stored terms version matching the current one or a confirmed profile field. Decide what happens if the user cancels or the check cannot run. Your security, product and compliance owners should sign off on this definition. The output is a short requirement record with the completion condition written as a testable statement.

  2. Implement the provider and factory classes

    Write a Java provider that implements the Required Action SPI, plus a factory that Keycloak uses to create it. The provider handles the evaluate step that decides whether the action is needed, the form step that renders the page, and the action step that processes the submitted input and clears the requirement. Target the Keycloak version your environment runs, because SPI method signatures can change between releases. The output is source code that compiles against that version and a unit test for the completion logic.

  3. Register the provider service and package the JAR

    Declare the factory in the provider service file so Keycloak discovers it at startup, then build the JAR with your standard build tooling. Keep dependencies out of the artifact unless the server does not already include them. Check that the provider ID you chose is stable, because realm configuration refers to it. The output is a versioned JAR and a checksum recorded in your artifact store.

  4. Add a theme template for the form if needed

    If the action needs its own page, such as a terms screen or a profile confirmation form, add a FreeMarker template to a custom login theme and reference it from the provider. Reuse the default theme's layout and message bundles so the page matches the rest of the sign-in flow and stays cheap to maintain across upgrades. Skip this step when the action only needs a redirect or a server-side check. The output is a theme directory with the template and a rendered page reviewed in a browser.

  5. Deploy the JAR and register the action in the realm

    Copy the JAR into the server's providers location in a non-production environment, restart or rebuild the server as your deployment method requires, and confirm the provider appears in the realm's required actions list. Set the action as enabled, decide whether it is a default action for new users, and set its priority relative to other required actions. Record these settings as realm configuration in your change system. The output is a realm configuration record that can be exported and reapplied.

  6. Test per-user triggers

    Assign the required action to a test user through the admin console or the Admin REST API, then sign in and confirm the step appears after authentication and blocks token issuance until completion. Repeat with the action cleared, with cancellation, and with a failed submission. Confirm the stored state changes as your completion condition requires. The output is a test matrix listing each case, the expected result and the observed result, with failures logged for fixing.

  7. Test application-initiated actions

    Trigger the action from a client by sending the required action parameter in the authorization request, and confirm that Keycloak runs the step for that session only. Check that the action is not left set on the user record after the session ends unless your design intends it. Also verify behavior for users who already satisfy the condition and for clients that do not request the action. The output is an extended test matrix that covers application-initiated paths alongside per-user paths.

  8. Confirm upgrade exposure before production rollout

    Re-run the test matrix on a staging copy of production with the exact Keycloak version planned for release, and review the upgrade notes for that version for changes to authentication or required action behavior. Confirm who owns rebuilding the JAR when the server is upgraded and who reviews the changelog. Promote the JAR and realm record together so the deployed artifacts match what was tested. The output is a signed-off staging report and a production change ticket tied to a specific provider version.

How to trigger the action per user and from applications

There are two ways to make a required action run, and they answer different questions. The first is per-user assignment: an administrator or a script sets the action on individual user records, either in the admin console or through the Administration REST API, and Keycloak presents the step the next time that user signs in. This suits one-off or targeted cases, such as asking a single account to confirm a profile field after a data correction. The second is application-initiated: a client asks for the step during its OpenID Connect authorization request, so the step applies to the sign-in that client starts. Before you build either path, confirm the exact parameter names for the action and any prompt values in the documentation for the Keycloak version you run, because these names have changed across releases and the upgrading notes are the place to check. Use the Keycloak server administration guide as the reference for the current behavior of user required actions.

Ordering needs a deliberate design. When several required actions are pending for one user, Keycloak runs them in the order the realm defines and the user cannot skip them, so a terms acceptance should normally come before a profile confirmation that depends on the updated terms. Confirm how your version orders assigned actions against the ones a user has already completed. Required actions also run inside the browser flow, which means conditional authentication can change when they appear. An authenticator condition that adds an extra factor or skips a step can place your action before or after that step, so test the combination of your flow, your conditions and your action for each client that shares the realm. Write down the expected sequence for each path, including what happens when an application asks for an action the user has already been assigned, and verify that sequence in a staging realm before production use.

Questions teams ask before writing required actions

Can a built-in required action do the job, or do we need a custom one?

Start with the built-in required actions in the Keycloak admin console. If your need is a standard step such as updating a password, configuring an OTP credential, or verifying an email address, the built-in action is usually the right choice and carries no custom code to maintain. Write a custom required action only when the step itself is specific to your business, such as showing updated terms that must be accepted, or confirming profile fields your own systems own. Check the current action list in the server administration guide for your release before you decide, because the set changes between versions. The deciding question is whether the step needs data or screens Keycloak does not already provide.

How much effort does a custom required action take, and what drives it?

The effort depends mostly on the number of screens, the data the step reads and writes, and how many applications and realms it must serve. A minimal action has a provider class, a factory, a registration in the server, and a form the user sees. Effort grows when the step calls an external system, stores acceptance records for audit, handles multiple languages, or needs a theme template that matches your login pages. Ask for a written scope that lists each screen, each external call, each stored attribute, and the test cases for users who skip, fail, or abandon the step. Those items are what a quote should price.

What ongoing upkeep does custom required action code create across Keycloak upgrades?

The code depends on Keycloak's SPI interfaces and on theme templates, so each upgrade can change what it needs. The Keycloak project says breaking changes in a minor release are opt-in, which lowers the risk of surprise, but the team still has to rebuild the provider JAR, retest the flow after each upgrade, and review the upgrade notes for changes to the interfaces or the templates your form extends. Keep the step small, avoid overriding more template files than the screen needs, and keep the test suite that exercises the step running in a non-production realm. Budget for this as recurring work tied to your upgrade cadence, not as a one-time build.

Can our in-house team build and maintain a custom required action?

Yes, if the team has Java experience, can run a Keycloak test environment, and has an owner who can define the step's business rules. The build itself is a small provider using the Java SPI, packaged as a JAR and deployed to the server, so a developer who knows Java and Keycloak's deployment model can produce it. The harder parts are the test coverage across user states, the upgrade routine, and the security review of anything the step stores or calls. Decide who on your staff will own the code after launch, because a provider that only one engineer understands becomes a risk when that person leaves.

What skills and inputs do we need from our own staff?

Three groups of people contribute. Engineers need Java and a working knowledge of Keycloak's authentication flows and deployment, plus access to a non-production Keycloak that matches production's version and database. A product or legal owner must decide the exact content of the step, such as which terms version applies, what wording users see, and what evidence of acceptance must be kept. Security or compliance staff should review where acceptance data is stored and who can read it. Before work starts, agree in writing on who signs off on the terms text, since changes to that text after build often cost more than code changes.

How is the cost of a custom required action quoted?

A fixed-scope quote follows from a written scope, so ask for one before work starts. DeployOpen does not publish a price list, and a quote reflects the scope you agree. Ask the quote to separate one-time build work (design, provider code, template changes, testing, deployment), recurring work (upgrade retests, provider rebuilds, support), and any external integration work on the systems the step calls. Also ask what is excluded, such as legal review of the terms text, and how changes to scope after sign-off are handled. Comparing quotes is easier when each one lists the same screens, flows, and test cases.

Does a required action work for every application, and how do we trigger it for one user?

A required action is a step attached to a user, and Keycloak runs it after login when the user has that action pending. You can set it per user in the admin console, through the Admin REST API, or through a flow that adds the action based on conditions. Applications can also request an action at sign-in with the kc_action parameter on the authorization request, which is useful for a one-off step such as a profile check tied to a single app. Confirm the exact parameter behavior and supported values for your Keycloak version in the server administration guide before building on it, and test both the per-user and per-application paths.

What drives the effort and how to keep it down

This table lists the factors that change the cost and effort of a custom required action and the practice that keeps each one under control. Read each row as a lever: the driver is what grows the work, and the reduction is what your team or partner can do about it. The main takeaway is that most effort comes from new forms, external data and untested user states, so keeping those to a minimum matters more than the Java code itself.

DriverWhy it changes effortHow to keep it down
Number of distinct actionsEach action needs its own provider, factory registration, tests and upgrade check, so effort grows with every added step.Combine related checks into one action where the user experience allows, and retire actions that no longer serve a purpose.
External data or a new formA call to a terms service or a new page adds network handling, error paths and template work beyond the provider logic.Reuse the built-in form pattern and keep external calls out of the request path where possible, or confirm the data source before the build starts.
Multi-language textEvery message key needs translations, and each locale must be checked in the rendered page.Limit the number of new strings, reuse existing message bundle keys, and confirm which languages the realm supports before writing text.
Testing across user states and clientsUsers who have some, all or none of the action completed, and clients with different flows, each behave differently and multiply test cases.Write a short test matrix before coding that lists user states, clients and login paths, and automate the cases that repeat on every release.
Production database and clustering effectsAction state is stored with the user, and changes must behave correctly across nodes and during rolling restarts.Keep the action idempotent, avoid long-running work in the action step, and confirm behavior on a clustered test environment that matches production.
Upgrade cadenceKeycloak releases on a set schedule, and SPI interfaces or template variables can change between versions, so custom code needs regular review.Keep the provider small, avoid overriding internal templates, pin a tested version in your plan, and budget a compatibility check for each upgrade.

How required actions connect to the rest of the platform

A required action rarely works alone. It reads and writes user data, renders theme pages, gets triggered by client settings and admin tools, and leaves a trail in events. Each connection below is a place where a change to one component can break the step users see after sign-in.

User profile and attributes

The action usually reads or writes data that the declarative user profile defines, such as accepted terms version or a confirmed phone number. Confirm that the attribute exists in the user profile, that its permissions let the action and the user change it, and that unmanaged attributes are handled deliberately. An attribute written by the action but missing from the profile can be lost or hidden in the admin console.

Keycloak docs

Theme templates the action renders

Each required action that shows a form renders a FreeMarker template from the login theme, so the page inherits the realm or client branding. Check which template name the action uses, whether your custom theme overrides it, and how the page behaves in each supported locale. A template renamed during a theme upgrade fails only for users who hit that action.

Client configuration that requests actions

Actions reach users through the realm's required action settings, through a user's assigned required actions, or through a client request that sends the user into the flow. Confirm which path each action uses and which clients can trigger it. An action enabled in the realm but never requested by any client may never run, and a client that requests an action without the action enabled fails in ways that are hard to trace.

Admin and Account Console triggers

Administrators can assign actions to a user in the admin console, and users can see some actions in the Account Console. Decide which of these paths your process needs, then test each one with a real user record. Admin-assigned actions and self-service actions can overlap, so agree on who may remove an action once it is set and how that is recorded.

Event listeners for completion audit

Required action completion and failure produce events that an Event Listener SPI provider or the built-in event log can capture. Decide which events your compliance evidence depends on, confirm that event storage is enabled for them, and check that a failed or abandoned action is recorded. Without this, you can show that the action exists but not that users completed it.

Identity brokering and first-login data

When users arrive through an upstream identity provider, first-login flows may already ask for fields such as email or name, and a custom action can run alongside them. Map which data the broker supplies, which the action must collect, and whether the action should run for brokered users at all. Test the first login from each upstream provider, since a missing mapper leaves users stuck on a form they cannot complete.

Keeping a custom required action working across upgrades

A custom required action is server-side code loaded into Keycloak through the Required Action SPI, so every Keycloak release is a potential change to the code your users depend on. Treat each upgrade as a retest of the provider, not just a server restart. Before any upgrade, rebuild the provider JAR against the target Keycloak version, deploy it to a test environment, and walk through the full flow: the login that triggers the action, the form or page it renders, the required action's completion, and the token or session state that follows. Confirm that the action still appears in the realm's Required actions tab and is still enabled for the intended realm. Read the upgrade notes for the target release and check every API, class or message key your provider touches against them.

Keycloak ships minor releases several times a year, and the project states that breaking changes in a minor release will be opt-in (per the Keycloak blog, October 2024). That policy lowers the risk of a surprise break, but it does not remove the need for retesting, because a provider compiled against an internal class can still fail when a dependency moves. The current release line at the time of writing is 26.8, so the practical plan is to schedule a retest against each minor release you intend to adopt and to avoid skipping several releases at once. Check the Keycloak upgrading guide for the exact version you are moving to, because it lists the changes that matter for SPI authors.

Control the rollout with the Keycloak Operator if you run it on Kubernetes. The Operator documentation strongly recommends manual approval mode for operator updates, which stops an upgrade from landing before your test results are in. Keep a test environment that mirrors production in its Keycloak version, database engine, realm configuration, custom theme and provider JARs, and identity provider settings, so that a passing test means something. Your provider rollout plan should record which JAR version runs in which environment, who approves promotion, and the rollback path if a new release breaks the action after go-live. Ask your implementation partner or internal team to confirm how the rollback handles database schema changes, since a schema migration can make a simple binary rollback unsafe.

How a required action runs during login

The flow starts when a user submits valid credentials. Keycloak then checks the user's pending required actions and any action the client requested, and routes the user to the matching provider, which renders its form and validates the input. If the user cannot complete the step, the login stops and the action stays pending. When the provider succeeds, the action is cleared, a completion event is recorded, and only then are tokens issued. Check that the failure path and the event are tested, because they are where custom actions most often break.

Sequence of a custom required action from pending check through token issuance.

How DeployOpen works on this

DeployOpen plans, implements, tests, documents and supports self-hosted open-source platforms inside your own infrastructure, including Keycloak. For custom required actions, we start by agreeing in writing which user step is needed, which applications and users it touches, and how it will be triggered, then build and verify it in a non-production realm before it reaches your users.

How we scope

  • The written scope names each required action, its trigger rules, the realms and clients it applies to, and the user data it reads or stores.
  • We confirm which Keycloak version and deployment model you run, since the SPI interfaces and upgrade exposure depend on them.
  • Engagements are fixed-scope, so deliverables and timelines are agreed in writing before work starts. A quote follows from that scope.

How we build

  • We implement the provider and factory classes, packaging, and enablement in your realm, with the testing environment matching your production setup.
  • Each trigger path is tested, including first sign-in, re-login after a terms version change, and application-initiated actions.
  • We check failure paths such as users who abandon the step, expired sessions, and users missing required profile fields.

How we hand over

  • You receive the source code, build instructions, and a deployment runbook that your platform team can follow without us.
  • Documentation covers how to enable the action, how to trigger it per user, and the upgrade checks to run before each Keycloak version change.
  • Written findings and a retest record are provided where security testing is part of the scope.

How we support

  • Ongoing support, upgrades and continuous testing are available as retainers, so the action keeps working across Keycloak minor releases.
  • Before you move to a newer Keycloak release, we can review the SPI changes that affect your custom action and plan the update.
  • Confirm with us, in the quote, who owns on-call response and how changes to the step's wording or versions are requested and approved.

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.