spring-ldap之为每次使用的新连接动态设置 spring ldap base

pander-it 阅读:117 2025-02-15 21:57:57 评论:0

这个问题是关于最佳实践的,如果可能的话。

我需要知道我是否可以在代码中动态更改 ldap 上下文源的基础?

我的 ldap bean 连接了以下内容

<ldap:context-source 
    url="ldap://<url>" 
    base="dc=example,dc=local" 
    username="<user>@example.local" 
    password="<pass>" 
/> 

我可以在代码中根据给定的动态变化参数将上下文源更改为另一个基础吗?

例如,如果我想将 base 更改为 dc=example2,dc=local 。

如果我以编程方式设置 LdapContextSource,这将没有问题。

请您参考如下方法:

所以这比我想象的更简单、更容易。

我所要做的就是继续创造

LdapContextSource ctxSrc = new LdapContextSource(); 
    ctxSrc.setUrl("ldap://<url>"); 
    ctxSrc.setBase("dc=example,dc=local"); 
    ctxSrc.setUserDn("<user>@example.local"); 
    ctxSrc.setPassword("<pass>"); 
 
ctxSrc.afterPropertiesSet(); // this method should be called. 
 
LdapTemplate tmpl = new LdapTemplate(ctxSrc); 
setLdapTemplate(tmpl); 

并将我的 LdapContextSource 值基于在我的例子中是动态源的属性。

我在想 Spring 更喜欢做一些事情。


标签:Spring
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号