我的系统使用acegi登录认证,并且配置用户缓存
<!-- 缓存器,为userCacheBackend提供缓存管理。 -->
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
<!-- EhCache一般用途如下:Hibernate缓存,DAO缓存,安全性凭证缓存(Acegi),Web缓存,应用持久化和分布式缓存。 -->
<bean id="userCacheBackend"
class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="cacheManager" />
</property>
<property name="cacheName">
<value>userCache</value>
</property>
</bean>
<!-- 用户缓存器,为daoAuthenticationProvider认证器提供缓存管理。 -->
<bean id="userCache"
class="org.springframework.security.providers.dao.cache.EhCacheBasedUserCache">
<property name="cache">
<ref local="userCacheBackend" />
</property>
</bean>
<!-- dao层认证,通过userDetailService获得用户信息 -->
<bean id="daoAuthenticationProvider"
class="org.springframework.security.providers.dao.DaoAuthenticationProvider">
<property name="userDetailsService">
<ref local="jdbcDaoImpl" />
</property>
<property name="userCache">
<ref local="userCache" />
</property>
</bean>
当用户成功登录后,登录信息被缓存在userCache,减少数据库的读取,如果想要清除缓存中数据,可以用以下方法:
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
//用户登录信息存在cache中,因此需要清除cache
CacheManager mange=(CacheManager)wac.getBean("cacheManager");
if (mange.cacheExists("userCache")) {
mange.getCache("userCache").removeAll();
}
本文介绍了一个基于Acegi的安全框架实现的登录认证系统,并详细展示了如何配置用户缓存来提升性能。文中提供了具体的Spring配置示例,包括缓存管理器的设置以及如何在登录后清除缓存中的数据。
166

被折叠的 条评论
为什么被折叠?



