是否可以在运行时更改LdapTemplate的LdapContextSource?

时间:2019-01-08 21:09:00

标签: java spring ldap

我正在尝试在应用程序启动期间在运行时更改LdapContextSource中的LDAP URL。

代码非常简单:

@Configuration
public class RepositoryConfiguration {

    private final int port;

    @Autowired
    public RepositoryConfiguration(LdapTemplate ldapTemplate)
            throws LDAPException {

        this.port = 12345; // new correct port
        configureLdapTemplate(ldapTemplate, port);
    }

    private void configureLdapTemplate(LdapTemplate ldapTemplate, int port) {
        LdapContextSource ldapContextSource = (LdapContextSource) ldapTemplate.getContextSource();
        ldapContextSource.setUrl("ldap://localhost:" + port);
        ldapContextSource.setAnonymousReadOnly(true);
    }
}

但是,当尝试对用户进行身份验证时,出现以下异常:

javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such Object]

请注意,当我不尝试以这种方式更改端口时,相同的LDAP存储库(以及我正在代码中其他地方进行身份验证的凭据)可以正常工作。

我的LDAP AuthenticationProvider使用LdapTemplate如下:

ldapTemplate.authenticate(authenticationBase, filter.encode(), password);

当然,它也被注入相同的自动连线的LdapTemplate。

我想知道我是错误地使用setContextSource方法还是Spring不打算使用的方式。任何人都可以鸣叫吗?

0 个答案:

没有答案
相关问题