przetestować połączenie LDAP przy użyciu uwierzytelniania Wiosna LDAP:
czyli z authenticate() metoda:
ldapTemplate.authenticate(query, password);
lub nawet lepiej, z getContext() metoda:
ldapTemplate.getContextSource().getContext(userDn, userPassword));
Catch the org.springframework.ldap.CommunicationException, by sprawdzić, czy połączenie się powiedzie.
Pełne fragment kodu powinien wyglądać następująco:
// Create the spring LdapTemplates; i.e. connections to the source and target ldaps:
try {
// Note: I'm using the direct LdapTemplate initialization rather than with bean creation (Spring ldap supports both)
log.info("Connecting to LDAP " + sourceHost + ":" + sourcePort + "...");
LdapContextSource sourceLdapCtx = new LdapContextSource();
sourceLdapCtx.setUrl("ldap://" + sourceHost + ":" + sourcePort + "/");
sourceLdapCtx.setUserDn(sourceBindAccount);
sourceLdapCtx.setPassword(sourcePassword);
sourceLdapCtx.setDirObjectFactory(DefaultDirObjectFactory.class);
sourceLdapCtx.afterPropertiesSet();
sourceLdapTemplate = new LdapTemplate(sourceLdapCtx);
// Authenticate:
sourceLdapTemplate.getContextSource().getContext(sourceBindAccount, sourcePassword);
} catch (Exception e) {
throw new Exception("Failed to connect to LDAP - " + e.getMessage(), e);
}
Uwaga: Używam sprężyny wersji 2.3.x LDAP:
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
</dependency>
dziękuję Marcel. Spróbuję twojego przykładu. – user1366786
Twój przykład zadziałał dobrze. A także, próbowałem to samo z wiosną DefaultSpringSecurityContextSource. – user1366786
Artykuł wymieniony powyżej na nieistniejącej już witrynie java.net (LdapTemplate: Programowanie LDAP w języku Java Made Simple) został przeniesiony do tego adresu URL: https://community.oracle.com/docs/DOC-983546 –