Multiple WebSecurityConfigurerAdapter in spring boot for multiple patterns
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Older Spring Security applications often used multiple WebSecurityConfigurerAdapter classes to apply different security rules to different URL patterns. The basic idea is to scope each configuration to a matcher, then order those configurations so the most specific one runs first.
That approach still appears in legacy Spring Boot codebases even though new projects should usually use SecurityFilterChain instead.
How Multiple Configurers Work
When you define more than one security adapter, Spring evaluates them in order and applies the first configuration whose request matcher fits the incoming request. That means two things matter immediately:
- each adapter must match only the URLs it is responsible for
- the order must go from most specific to most general
A common split looks like this:
- '
/api/**uses HTTP Basic or token-based auth' - '
/admin/**requires an admin role' - everything else uses form login
If a broad matcher runs too early, it can swallow requests meant for a narrower configuration.
Legacy Example With Two Adapters
Here is a classic adapter-based setup:
The first adapter handles only /api/** requests because of antMatcher. The second acts as the fallback configuration for everything else.
Why Order Matters
Suppose the form-login configuration were evaluated before the API configuration. Then a request to /api/orders might be caught by the general rule and redirected to a login page instead of getting HTTP Basic authentication.
That is why ordering is not optional here. Specific rules must come before fallback rules, or the entire split-security design collapses into confusing behavior.
A useful mental model is "first matching security configuration wins."
The Modern Equivalent
WebSecurityConfigurerAdapter is deprecated in modern Spring Security. New code should define multiple SecurityFilterChain beans instead. The concept is the same even though the API is newer:
If you are maintaining legacy code, understanding the adapter approach is still valuable. If you are writing new code, prefer filter chains.
Common Pitfalls
The most common mistake is forgetting to scope a configuration. Without antMatcher or another request matcher, one adapter can accidentally apply to the whole application and shadow every other one.
Another pitfall is getting the order wrong. The more general configuration must not come before the narrow one.
A third issue is mixing browser-style and API-style expectations. Redirects, CSRF handling, session state, and authentication entry points often differ between UI traffic and API traffic. Splitting configurations helps, but only if the boundaries are clear.
Summary
- Multiple
WebSecurityConfigurerAdapterclasses were a legacy way to secure different URL patterns differently. - Each adapter needs a clear matcher so it handles only the intended requests.
- Ordering is essential because the first matching configuration wins.
- Broad fallback rules should come after specific API or admin rules.
- New Spring Security code should usually use multiple
SecurityFilterChainbeans instead.

