Spring Security LDAP and Remember Me
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Spring Security can authenticate users against LDAP and still provide "remember me" login persistence through a cookie. The important point is that these are different concerns: LDAP verifies the user's identity, while remember-me stores enough information to restore authentication on later requests.
LDAP And Remember-Me Solve Different Problems
LDAP answers:
- who is the user
- are the credentials valid
- what groups or roles should be loaded
Remember-me answers:
- should the browser stay signed in across sessions
- can Spring recreate authentication from a trusted cookie
That means enabling remember-me does not replace LDAP. It sits on top of the normal authentication process.
A Typical Spring Security Configuration
With modern Spring Security, the usual setup is a SecurityFilterChain bean.
This shows the remember-me side. The LDAP part still needs an authentication setup that loads users and authorities from the directory.
Configure LDAP Authentication
One common approach is to use an LDAP authentication provider with a context source.
The exact LDAP setup varies with your directory structure, but the overall flow is stable:
- user logs in with username and password
- Spring Security validates against LDAP
- Spring creates an authenticated session
- remember-me optionally writes a cookie for future requests
How Remember-Me Works After LDAP Login
Once the user authenticates successfully with LDAP, Spring can write a remember-me cookie if the login form includes the remember-me parameter.
A simple login form field:
On a later visit, Spring reads the cookie and rebuilds the authentication without forcing a fresh username-password login every time.
For that to work correctly, Spring still needs a UserDetailsService or equivalent way to load the user when the remember-me token is presented. That is why remember-me configuration often points to a user-details service even when the initial sign-in came from LDAP.
Token Strategy Matters
Spring supports different remember-me strategies. The hash-based token is simple, but persistent tokens stored in a database are often better for revocation and auditing.
For example, a persistent token repository lets you invalidate tokens server-side rather than depending only on the cookie's integrity.
That matters in enterprise systems using LDAP because the directory may define identity, while the web application still needs its own control over browser persistence and logout behavior.
Security Considerations
Remember-me is a convenience feature, so it carries tradeoffs.
Use it carefully:
- always require HTTPS
- mark cookies secure and HTTP-only
- use a strong remember-me key
- keep the validity window reasonable
- understand that disabling the LDAP password alone may not invalidate a previously issued remember-me cookie unless your design rechecks account state appropriately
That last point is important. Authentication persistence should not silently outlive account policy changes.
Common Pitfalls
The biggest mistake is assuming remember-me authenticates against LDAP by itself. It does not. It only restores authentication after a previous successful login.
Another mistake is enabling remember-me without a reliable user-loading strategy. Spring still needs a way to reconstruct the authenticated principal from the token.
People also forget cookie security. A convenience cookie without HTTPS and proper flags weakens the whole login flow.
Finally, do not treat remember-me as equivalent to a long-lived session for sensitive admin systems without reviewing revocation and account-disable behavior carefully.
Summary
- LDAP handles identity verification, while remember-me handles browser login persistence.
- The two features work together but solve different problems.
- Configure LDAP authentication first, then add remember-me on top of the normal login flow.
- Remember-me still needs a dependable way to reload user details later.
- Use secure cookies, strong keys, and a reasonable token lifetime.
Related reading
- Spring Security mapping OAuth2 claims with roles to secure Resource Server endpoints
- Spring security method cannot decide pattern is MVC or not Spring Boot application exception
- Spring Security, Method Security annotation Secured is not working java config
- Spring Test returning 401 for unsecured URLs
- Spring server.forward-headers-strategy NATIVE vs FRAMEWORK
- Spring spring.profiles.include overrides
- SpringBoot 401 UnAuthorized even with out security
- Springboot endpoint 403 OPTIONS when doing a POST request

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.