Unable to use Keycloak in Spring Boot 2.1 due to duplicated Bean Registration httpSessionManager
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A duplicated httpSessionManager bean error in Spring Boot 2.1 with Keycloak usually means two configuration paths are trying to register the same bean name. Spring Boot 2.1 also tightened default behavior around bean overriding, so setups that worked in older versions can fail at startup. The fix is to choose one ownership model for security beans and remove or rename the duplicate registration.
Why This Error Appears
The startup failure normally happens when both Keycloak adapter configuration and custom security configuration declare overlapping beans. In earlier projects, bean overriding might have hidden this issue. In Boot 2.1, duplicate names fail fast unless overriding is explicitly allowed.
Typical symptoms include:
BeanDefinitionOverrideException- Startup log mentions
httpSessionManager - Application context stops before web server is ready
Reproduce the Conflict in a Small Example
The following configuration shows the pattern that causes trouble.
With this setup, Boot 2.1 will fail because both beans use the same name.
Preferred Fix: One Bean Owner
Keep exactly one httpSessionManager bean definition. If Keycloak auto-configuration already provides it, remove your local duplicate. If you need custom behavior, define your own bean and disable the conflicting auto path.
A safe explicit approach is:
Then ensure no second configuration class declares the same bean name.
Temporary Escape Hatch
You can allow bean overriding, but this should be a short-term migration step, not a permanent fix.
Use this only while auditing configuration ownership. Silent overriding can hide security misconfiguration, especially in authentication flows.
Align Library Versions
Many failures come from incompatible adapter and framework versions. Keep Keycloak Spring adapter and Spring Boot versions aligned with the adapter compatibility matrix for your dependency set.
A minimal Maven example:
Version pinning keeps transitive security classes predictable across environments.
Diagnostic Workflow That Saves Time
Use a startup checklist:
- Search codebase for
httpSessionManagerbean definitions - Inspect imported security config classes
- Check active Spring profiles for duplicate config loading
- Print auto-configuration report for conflicting paths
- Verify dependency tree for multiple Keycloak adapter versions
Command example:
If you find two adapter versions, resolve that before deeper debugging.
Modernization Note
For long-term maintenance, many teams migrate away from legacy Keycloak adapters and implement OAuth2 resource server and client flows with Spring Security first-class support. This reduces custom adapter interactions and startup conflicts. Even if migration is not immediate, isolating Keycloak integration behind one configuration module will reduce repeated bean registration issues.
Common Pitfalls
- Defining
httpSessionManagerin multiple@Configurationclasses - Enabling override permanently and masking real startup conflicts
- Mixing incompatible Keycloak and Spring Boot versions
- Loading duplicate config through profile-specific imports
- Debugging only logs without checking dependency tree and bean names
The issue is usually deterministic once bean ownership is made explicit.
Summary
- Boot 2.1 fails fast when two beans share the same name.
- Duplicate
httpSessionManagerregistrations are the primary cause. - Keep one bean owner and remove or rename duplicates.
- Use override mode only as a temporary migration tool.
- Validate versions and dependency tree to prevent recurring conflicts.

