Spring multiple authentication methods for different api endpoints
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Many Spring applications need different authentication methods for different endpoint groups. Public API consumers may use JWT bearer tokens, internal tools may use HTTP Basic, and admin pages may use form or OAuth login. Spring Security supports this cleanly with multiple filter chains matched by request patterns.
Use Multiple SecurityFilterChain Beans
In Spring Security 6 style configuration, define separate SecurityFilterChain beans with explicit matchers and order. The first matching chain handles the request.
This pattern keeps endpoint intent explicit and avoids one oversized security rule block.
Authentication Providers and User Sources
Different chains can still share providers where appropriate. For example, Basic auth endpoints may use in-memory or LDAP users, while JWT endpoints rely on token signature validation and claim mapping.
If your JWT uses custom claim names for roles, configure a converter so authorities map consistently.
Consistent authority mapping prevents authorization surprises across endpoint groups.
Endpoint Design and Documentation
Security complexity drops when routes are clearly segmented. Keep URL namespaces clean:
/api/publicfor anonymous endpoints./api/privatefor JWT-protected business APIs./internalfor operational tools.
Document expected authentication type per namespace in API docs. Client integration issues often come from unclear auth expectations rather than broken security code.
Add integration tests that verify both success and failure for each endpoint family. Tests should assert status codes, not only controller logic.
Migration Notes
If your codebase still uses WebSecurityConfigurerAdapter, migrate gradually to bean-based filter chains. The new style is easier to reason about and aligns with current Spring Security recommendations.
During migration, keep one explicit fallback chain that denies unmatched traffic. This protects against accidentally exposed routes when path matchers are incomplete.
For larger systems, create a security architecture matrix that maps each endpoint group to authentication type, token issuer, required authorities, and expected client type. This simple artifact reduces onboarding time and helps reviewers catch mismatched matcher rules before they reach production safely. It also supports audit conversations by making endpoint security intent visible outside source code.
Common Pitfalls
A common pitfall is overlapping request matchers with incorrect @Order. If a broad matcher is evaluated first, specific chains may never run.
Another issue is mixing stateful and stateless auth defaults. JWT endpoints are usually stateless, while form or session flows are stateful. Configure session policy deliberately per chain.
Developers also forget CORS and CSRF differences by endpoint type. Browser clients and machine clients often need different settings, so apply them intentionally per route group.
Finally, teams skip security integration tests and rely on manual checks. This makes regressions likely when endpoints are added or reorganized.
Summary
- Define multiple
SecurityFilterChainbeans with explicit matchers and order. - Map each endpoint namespace to one clear authentication mechanism.
- Configure authority conversion consistently for token-based auth.
- Keep a deny-all fallback chain to prevent accidental exposure.
- Add integration tests that validate auth behavior per endpoint family.
Related reading
- spring mvc rest service redirect / forward / proxy
- Spring Non-blocking Rest Send and forget
- Spring OAuth redirect_uri not using https
- Spring RestTemplate - how to enable full debugging/logging of requests/responses?
- Spring Security 5 No Beans of type BCryptPasswordEncoder found
- Spring Security 5 Replacement for OAuth2RestTemplate
- Spring MVC - How to get all request params in a map in Spring controller?
- Spring MVC - How to return simple String as JSON in Rest Controller

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.