-
-
Save mpilone/7582628 to your computer and use it in GitHub Desktop.
// Setup the LDAP client (normally done via Spring context file). | |
LdapContextSource contextSource = new LdapContextSource(); | |
contextSource.setUrl("ldap://adserver.mycompany.com:3268"); | |
contextSource.setBase("DC=AD,DC=MYCOMPANY,DC=COM"); | |
contextSource.setUserDn("[email protected]"); | |
contextSource.setPassword("password1"); | |
contextSource.afterPropertiesSet(); | |
LdapTemplate ldapTemplate = new LdapTemplate(contextSource); | |
ldapTemplate.afterPropertiesSet(); | |
// Perform the authentication. | |
Filter filter = new EqualsFilter("sAMAccountName", "mpilone"); | |
boolean authed = ldapTemplate.authenticate("OU=CorpUsers", | |
filter.encode(), | |
"user-entered-password"); | |
// Display the results. | |
System.out.println("Authenticated: " + authed); |
This worked for me, thank you.
Hi, are there any samples to load my truststore cert while connecting ldap using spring ldap, i tried below code, but getting PKIX exceptions on authenticate. any solutions.
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl("ldap://adserver.mycompany.com:3268");
contextSource.setBase("DC=AD,DC=MYCOMPANY,DC=COM");
contextSource.setUserDn("[email protected]");
contextSource.setPassword("password1");
DefaultTlsDirContextAuthenticationStrategy strategy = new DefaultTlsDirContextAuthenticationStrategy();
strategy.setSslSocketFactory(sslFactory());
contextSource.setAuthenticationStrategy(strategy);
contextSource.afterPropertiesSet();
LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
ldapTemplate.afterPropertiesSet();
boolean authed = ldapTemplate.authenticate("OU=CorpUsers",
filter.encode(),
"user-entered-password");
Exception:
springframework.ldap.CommunicationException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.naming.CommunicationException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target [Root exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target]
at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:108)
at org.springframework.ldap.core.support.AbstractContextSource.doGetContext(AbstractContextSource.java:147)
at org.springframework.ldap.core.support.AbstractContextSource.getReadOnlyContext(AbstractContextSource.java:158)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:357)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:309)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:642)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:578)
at org.springframework.ldap.core.LdapTemplate.authenticate(LdapTemplate.java:1441)
at org.springframework.ldap.core.LdapTemplate.authenticate(LdapTemplate.java:1426)
at org.springframework.ldap.core.LdapTemplate.authenticate(LdapTemplate.java:1369)
Any suggestion to get the pagination working with large no of entries?
Hi, thanks for the very good example. Question - do you have a code which uses paged query to authenticate the user. For big LDAPs ldapTemplate.authenticate() method fails due to LDAP query limitation - 5000 entries max
Thanks, Sam
Thank's a lot
I use the above code for ldap authentication,But im getting the javax.naming.nopermissionexception: [ldap error code 50 anonymous access] remaining name "dc=test,com=ex".but at the same time im able to search the user and get their respective attributes
Thanks for your sharing, this is very helpful.