What a Keycloak password policy is
Every realm in Keycloak has one password policy. It decides which passwords users may choose and how Keycloak stores them. Without a policy, a realm accepts any password, including a single character.
The policy is stored as one string on the realm, in the passwordPolicy field. Each rule is a policy id with an optional value in brackets, and rules are joined by the word and. The admin console builds the same string when you add rules under Authentication, Policies, Password policy.
length(12) and maxLength(64) and digits(1) and notUsername(undefined) and notEmail(undefined) and passwordHistory(3) and hashAlgorithm(pbkdf2-sha512) and hashIterations(210000)This page explains each rule the builder offers, how Keycloak 26 applies them, and how to roll a policy out without locking anyone out.
Why the policy matters in production
Passwords are still the first factor for most realms. A weak policy lets users pick passwords that appear in breach lists, and those are the first ones an attacker tries.
The policy also decides the cost of a stolen database. The hashing rules set how much work an attacker must do for each guess against a leaked hash. A realm with good length rules but cheap hashing is still exposed if its database leaks.
Auditors ask about it too. Security reviews and customer questionnaires often ask for the minimum length, reuse rules and hashing method. The policy string answers all three in one line, which makes it easy to show and easy to keep in version control.
The rules this builder supports
The builder offers only policy ids that ship with Keycloak. Each one maps to a field in the form.
| Policy id | What it checks | Value |
|---|---|---|
length | Minimum number of characters | A number |
maxLength | Maximum number of characters | A number |
digits | Minimum number of digits | A number |
lowerCase | Minimum number of lower-case letters | A number |
upperCase | Minimum number of upper-case letters | A number |
specialChars | Minimum number of special characters | A number |
notUsername | The password is not the username | None |
notEmail | The password is not the email address | None |
passwordHistory | Not one of the last N passwords | A number |
forceExpiredPasswordChange | Expire the password after N days | Days |
regexPattern | The password matches a regular expression | A Java regex |
passwordBlacklist | The password is not in a deny list file | A file name |
hashAlgorithm | The algorithm for newly stored passwords | A provider id |
hashIterations | The iteration count for newly stored passwords | A number |
Rules without a value, such as notUsername, can be written with or without (undefined). The admin console writes notUsername(undefined), so the builder does too. That keeps a diff against an export from the console clean.
Length and character rules
Length is the rule that does the most work. A long minimum makes guessing far harder than a short password with a symbol in it. Character class rules push users toward patterns such as a capital first letter and a digit at the end, which attackers already expect.
A maximum length is still worth setting. It stops very long inputs from costing the server hashing time, and it gives users a clear limit to work with. Set it well above the minimum so passphrases fit.
History, expiry and deny lists
passwordHistory stops users from cycling back to a recent password after a forced change. forceExpiredPasswordChange makes a password expire after a number of days. Current guidance from many security bodies is to avoid routine expiry and change passwords only after a sign of compromise, so use expiry only when a rule you must follow asks for it.
passwordBlacklist names a file of banned passwords, one per line. The file lives on the server, in the data/password-blacklists folder of the Keycloak installation. Only the file name goes into the policy, never a path.
Hashing rules
hashAlgorithm and hashIterations decide how new passwords are stored. The builder offers pbkdf2-sha512, pbkdf2-sha256, pbkdf2 (SHA-1) and argon2 for recent versions. Leave both out to use Keycloak’s default for your version and mode.
The iteration hints in the builder are OWASP’s suggestions for PBKDF2: 210,000 for SHA-512, 600,000 for SHA-256 and 1,300,000 for SHA-1. They are not Keycloak defaults. When Argon2 is selected, the builder hides the iterations field, because Argon2 is tuned through its own settings.
How Keycloak 26 applies the policy
Keycloak checks the policy when a password is set or changed. That covers registration, the update password required action, the account console, the reset password flow and passwords set by an admin. If a new password breaks a rule, the user sees the reason and can try again.
Existing passwords are not checked at sign-in. A user whose password was set under an older, weaker policy keeps signing in with it until it changes. The one exception is expiry: forceExpiredPasswordChange is checked at sign-in and sends the user to the update password page when the password is too old.
Hashing rules work the same way. Keycloak verifies each stored password with the algorithm and iterations recorded in that credential, so changing hashAlgorithm never breaks existing sign-ins. New and changed passwords use the new settings.
What this tool does and does not do
The builder turns form fields into a valid policy string, and turns a policy string back into the form. It also writes the same policy as a line for a realm JSON file and as a kcadm.sh command.
- It checks that numbers are whole and at least 1, and that the maximum is not below the minimum.
- It checks that a regular expression parses, and refuses one that contains the word
and, which separates rules. - It checks that a deny list is a file name without a path.
- It runs in your browser and sends nothing anywhere.
It does not test passwords against the policy, and it does not know which rules your Keycloak version accepts beyond the ones listed. It cannot see your deny list file or check that it exists on the server. Rules it does not know are reported when you load an existing policy, and left out of the form.
How to use the builder
- Set a minimum length first, then add only the rules you need.
- Tick Not the username and Not the email address.
- Add a history count if users are ever forced to change passwords.
- Pick a hashing algorithm and iterations, or keep Keycloak’s default.
- Copy the policy string into the console, the realm file line into your import, or the
kcadm.shcommand into a script.
To change a policy you already have, paste it under Read an existing policy and load it. Edit the form, then copy the new string.
kcadm.sh update realms/acme -s 'passwordPolicy=length(12) and notUsername(undefined) and passwordHistory(3)'Where it pays off
A policy string is short, but it is easy to get wrong by hand. These are the moments when a builder helps most.
First production deploy
A new realm starts with no policy. Set one before the first user registers, so every password in the database meets it from day one. Put the string in your realm import file, so a rebuilt environment gets the same rules.
Security audit
Load the policy from the realm export or the console into the builder. The form shows each rule in plain words, which is easier to discuss than the raw string. If the hashing fields are empty, the realm uses the version default, and you can note that in the audit.
Moving between environments
Staging and production often drift. Keep the policy string in the realm file in version control and apply it with kcadm.sh or an import. The realm export diff shows when the two have gone apart.
Importing users from another system
Imported password hashes keep working whatever the policy says, because Keycloak checks them with the algorithm stored in each credential. Users who import with a temporary password, or none, meet the policy when they set their own.
Common errors and fixes
The policy string is rejected
A typo in a policy id, a missing bracket or a stray word makes the realm update fail. Load the string into the builder: rules it cannot read are listed, so you can see which part is wrong.
Users cannot reset their password
A strict regular expression or very high character counts can reject almost every password. Users then give up on the reset form. Test the policy with a few real-world passphrases before you roll it out.
The deny list has no effect
The file must be on every Keycloak node, in data/password-blacklists, under exactly the name in the policy. In a container, add it to the image or mount it. If a node cannot find the file, look for an error about it in that node’s log.
Sign-in got slower after a hashing change
More iterations mean more CPU for every password check. After raising them, watch sign-in latency and node CPU, and size your nodes for the peak sign-in rate, not the average.
Security notes
- A policy only protects passwords set after it exists. Plan a reset or wait for natural changes if old passwords were weak.
- A deny list of common and breached passwords blocks the guesses attackers try first.
- A strong policy does not replace a second factor. Pair it with OTP or WebAuthn for admins and sensitive apps.
- Turn on brute force detection too: a policy slows offline guessing, lockouts slow online guessing.
Best practices
- Prefer a long minimum, such as 12 or more, over several character class rules.
- Always add
notUsernameandnotEmail. - Set
maxLengthhigh enough for passphrases. - Use expiry only when a rule you must follow requires it.
- Keep the policy string in your realm file and in version control.
- Change hashing settings deliberately, and test sign-in load afterwards.