Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting and using Spring Security UserCache

Spring Boot here. I am implementing my own UserDetailsService impl and need to reference/use the Spring-provided UserCache:

package myorg.myapp;

import org.springframework.security.core.userdetails.UserCache;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class MyUserDetailsService implements UserDetailsService {
    @Resource
    private UserCache userCache;

    ...
}

When I go to run my app I get the following exception message:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of
type [org.springframework.security.core.userdetails.UserCache] found for dependency: expected at
least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER,
type=class java.lang.Object, mappedName=)}

Any ideas why Spring can't find a UserCache bean?

like image 410
smeeb Avatar asked Sep 08 '26 15:09

smeeb


1 Answers

You need to implement a UserCache as a @Bean somewhere in your code.

The NullUserCache is a 'fake' one that doesn't actually cache anything. Look at the code.

The SpringCacheBasedUserCache is probably what you want. It requires a Cache to be passed in the constructor. This cache can be something like a ConcurrentMapCache, EhCacheCache, or JCacheCache.

I find the ConcurrentMapCache great for local development.

I don't use the UserCache in anything I'm currently working on but here's my config for Object caching. In this @Configuration file you'll see 2 profiles, one for local dev and one for aws deployments. Hopefully this will get you going - https://gist.github.com/denov/021e9c736d15b1fc56c3e8d185b8ef15

like image 76
denov Avatar answered Sep 11 '26 04:09

denov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!