How to add LDAP cache in Spring LDAP?
我想在本地缓存 LDAP 用户数据以加快查询速度。 Spring LDAP 是否提供这样的功能?我该怎么做?
我正在使用 Spring Security 3.1 和 Spring LDAP 1.3.1 进行身份验证和授权。如果存在的话,使用内置机制为 LDAP 提供缓存会很好..
Spring LDAP 配置:
applicationContext-ldap.xml:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:jee=”http://www.springframework.org/schema/jee” xsi:schemaLocation=” http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd “> <!– Ldap –> <!– for authentication and search purpose –> <bean id=”ldapTemplate” class=”org.springframework.ldap.core.LdapTemplate”> <!– for pagination search purpose –> <bean id=”singleLdapContextSource” class=”org.springframework.ldap.core.support.SingleContextSource” scope=”session”> <bean id=”singleLdapTemplate” class=”org.springframework.ldap.core.LdapTemplate” scope=”session”> </beans> |
弹簧安全配置:
spring-security.xml:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:security=”http://www.springframework.org/schema/security” xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd”> <!– This is where we configure Spring-Security –> <security:form-login <security:logout <security:authentication-manager> |
非常感谢您的帮助!
如果您配置 EhCacheBasedUserCache 并使用 ldap-user-service 那么您可以将缓存用作:
1
2 3 4 |
<ldap-user-service
user-search-filter=”(sAMAccountName={0})” user-search-base=”dc=myDomain,dc=com” cache-ref=”userCache” /> </authentication-provider> </authentication-manager> |
我不认为 Spring 提供开箱即用的客户端 LDAP 缓存,因为在客户端缓存 LDAP 查询结果会带来安全风险。缓存肯定会在某个时候保存陈旧的数据,如果它是例如,这不是一个大问题。用户的电子邮件/家庭地址,但更糟糕的是,例如角色分配和其他身份验证/授权相关数据。通过扩展服务器端,您会变得更好,以便它能够处理负载。
也就是说,从 Spring 3.1 开始引入缓存非常容易,因为它为它提供了出色的支持。在您的情况下,使用自定义 LdapContextSource 就足够了,如下所示:
1
2 3 4 5 6 7 8 9 10 |
public class CachingLdapContextSource extends AbstractContextSource {
@Override |
package类只是将所有 DirContext 方法委托给底层实现,并用 @Cacheable 装饰要缓存的方法。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class CachingDirContextWrapper implements DirContext {
private final DirContext delegate; CachingDirContextWrapper(DirContext delegate) { @Override … |
参考官方文档,本教程详细介绍如何配置Spring使用的缓存存储。
但我认为,你最好不要这样做。
来源:https://www.codenong.com/16057854/