You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Using Spring Security 6.3.1 we're using LdapUserDetailsService to fetch user data and authorities. To decrease number of requests to LDAP infrastructure, we're using CachingUserDetailsService configured with LdapUserDetailsService as delegate, and SpringCacheBasedUserCache backed by Caffeine.
Caffeine is set up with "expireAfterWrite=1m" spec, so authorities of user are cached 1 minute after LDAP call.
The issue: delegate method LdapUserDetailsService::loadUserByUsername should be called every minute if user is sending requests in small intervals (<1 minute), but it's called only once per request batch - each access of user details in CachingUserDetailsService refreshes the cache with cached value:
@OverridepublicUserDetailsloadUserByUsername(Stringusername) {
UserDetailsuser = this.userCache.getUserFromCache(username);
if (user == null) {
user = this.delegate.loadUserByUsername(username);
}
Assert.notNull(user, () -> "UserDetailsService " + this.delegate + " returned null for username " + username
+ ". " + "This is an interface contract violation");
// FIXME it's putting value read from cache back into the cache, thus resetting expiryAfterWrite timeoutthis.userCache.putUserInCache(user);
returnuser;
}
Describe the bug
Using Spring Security 6.3.1 we're using LdapUserDetailsService to fetch user data and authorities. To decrease number of requests to LDAP infrastructure, we're using CachingUserDetailsService configured with LdapUserDetailsService as delegate, and SpringCacheBasedUserCache backed by Caffeine.
Caffeine is set up with "expireAfterWrite=1m" spec, so authorities of user are cached 1 minute after LDAP call.
The issue: delegate method LdapUserDetailsService::loadUserByUsername should be called every minute if user is sending requests in small intervals (<1 minute), but it's called only once per request batch - each access of user details in CachingUserDetailsService refreshes the cache with cached value:
CachingUserDetailsService
moving putUserInCache just after user is loaded from delegate inside the if block should fix the issue.
To Reproduce
Configure UserDetailsService as:
then require user detais more often that expiryAfterWrite cache timeout.
Expected behavior
Cache expires a minute after first requests, refreshes authorities from LDAP.
The text was updated successfully, but these errors were encountered: