Skip to content

Instantly share code, notes, and snippets.

@wytten
Last active January 11, 2019 06:29
Show Gist options
  • Select an option

  • Save wytten/10670050 to your computer and use it in GitHub Desktop.

Select an option

Save wytten/10670050 to your computer and use it in GitHub Desktop.
Simple Active Directory authentication with Spring LDAP
<?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:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
<ldap:context-source
url="ldap://adserver1.yourcompany.com:389, ldap://adserver2.yourcompany.com:389"
base="dc=yourcompany,dc=com"
username="...,dc=yourcompany,dc=com"
password="secret"
referral="follow"/>
<ldap:ldap-template id="ldapTemplate" />
</beans>
import static org.springframework.ldap.query.LdapQueryBuilder.query;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.AuthenticationException;
public class Example {
@Autowired
LdapTemplate ldapTemplate;
public boolean authenticate(String uid, String credentials) {
try {
ldapTemplate.authenticate(query().where("uid").is(uid), credentials);
return true;
} catch (AuthenticationException e) {
LOG.error("Login failed", e);
return false;
}
}
}
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment