二缓存用户信息
查看AuthenticationProvider接口的实现类AbstractUserDetailsAuthenticationProvider抽象类(我们配置文件中配置的DaoAuthenticationProvider类继承了该类)的源代码,会有一行代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
1
UserDetails user
=
this
.userCache.getUserFromCache(username);
DaoAuthenticationProvider认证提供者使用UserCache接口的实现来实现对用户信息的缓存,修改DaoAuthenticationProvider的配置如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
1
<
bean
id
="daoAuthenticationProvider"
2 class ="org.springframework.security.providers.dao.DaoAuthenticationProvider"
3 p:userCache-ref ="userCache"
4 p:passwordEncoder-ref ="passwordEncoder"
5 p:userDetailsService- ref ="userDetailsService" />
2 class ="org.springframework.security.providers.dao.DaoAuthenticationProvider"
3 p:userCache-ref ="userCache"
4 p:passwordEncoder-ref ="passwordEncoder"
5 p:userDetailsService- ref ="userDetailsService" />
这里我们加入了对userCache Bean的引用,userCache使用Ehcache来实现对用户信息的缓存。userCache配置如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
1
<
bean
id
="userCache"
2 class ="org.springframework.security.providers.dao.cache.EhCacheBasedUserCache"
3 p:cache-ref ="cache" />
4 < bean id ="cache"
5 class ="org.springframework.cache.ehcache.EhCacheFactoryBean"
6 p:cacheManager-ref ="cacheManager"
7 p:cacheName ="userCache" />
8 < bean id ="cacheManager"
9 class ="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
10 p:configLocation ="classpath:ehcache.xml" >
11 </ bean >
2 class ="org.springframework.security.providers.dao.cache.EhCacheBasedUserCache"
3 p:cache-ref ="cache" />
4 < bean id ="cache"
5 class ="org.springframework.cache.ehcache.EhCacheFactoryBean"
6 p:cacheManager-ref ="cacheManager"
7 p:cacheName ="userCache" />
8 < bean id ="cacheManager"
9 class ="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
10 p:configLocation ="classpath:ehcache.xml" >
11 </ bean >
我们这里使用的是EhCacheBasedUserCache,也就是用EhCache实现缓存的,另外系统还提供了一个默认的实现类NullUserCache类,我们可以通过源代码了解到,无论上面使用这个类都返回一个null值,也就是不使用缓存。