我需要使用spring提供的usercache,但似乎无法使其工作。据我所知,我甚至认为我在使用它时将其作为daoAuthenticationProvider(我试图使用它的功能)中的引用来获取userdetails服务的实现。我做了以下事情:
1)使用我自己的HibernateDao实现UserDetailsInterface
2)按照第1点的规定提供了我自己的loadbyusername(String username)方法
3)配置我的app-context:
<beans:bean id="encoder" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder"/>
<beans:bean id="userCache" class="org.springframework.security.core.userdetails.cache.EhCacheBasedUserCache">
<beans:property name="cache" ref="userCacheBackend"/>
</beans:bean>
<beans:bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<beans:property name="configLocation" value="classpath:/ehcache.xml"/>
</beans:bean>
<beans:bean id="userCacheBackend" class="rg.springframework.cache.ehcache.EhCacheFactoryBean">
<beans:property name="cacheManager" ref="cacheManager"/>
<beans:property name="cacheName" value="userCache"/>
</beans:bean>
<authentication-manager>
<authentication-provider user-service-ref="userDao">
<password-encoder ref="encoder" />
</authentication-provider>
</authentication-manager>
<beans:bean id="daoAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="userDao"/>
<beans:property name="passwordEncoder" ref="encoder"/>
<beans:property name="userCache" ref="userCache"/>
</beans:bean>
然而,尽管我将userDao设置为userDetailsService属性名称,但我似乎无法使用DaoAuthenticationProvider和AbstractUserDetailsAuthenticationProvider类提供的任何功能来实现UserDetailsService接口(这将是我的HibernateDao)。
这是我的hibernate dao:
@Repository("userDao")
public class HibernateUserDao extends HibernateBaseDao<User, Long> implements
UserDao, UserDetailsService{ etc ....................
@Transactional
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
logger.warn(" loadByUserName has been invoked for username " + username);
List users = getSessionFactory().getCurrentSession().
createQuery("from User where username=?")
.setParameter(0, username).list();
if (users == null || users.isEmpty()) {
throw new UsernameNotFoundException("user '" + username
+ "' not found...");
} else {
return (UserDetails) users.get(0);
}
}
我做的事真的很蠢吗?请帮助我疯了。