├── .gitignore ├── README.md ├── pom.xml └── src └── main └── java └── dk └── digitalidentity ├── Application.java └── app ├── ActiveDirectoryHelper.java ├── LdapConfig.java ├── LdapPerson.java └── PersonRepo.java /.gitignore: -------------------------------------------------------------------------------- 1 | .project 2 | .classpath 3 | /target 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spring-boot-minimal 2 | Boilerplate code for spring-boot projects 3 | # spring-boot-ldap-example 4 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | digitalidentity.dk 7 | spring-boot-minimal 8 | 1.0.0 9 | 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 1.2.3.RELEASE 14 | 15 | 16 | 17 | 18 | org.springframework.boot 19 | spring-boot-starter 20 | 21 | 22 | org.springframework.security 23 | spring-security-ldap 24 | 25 | 26 | 27 | 28 | 1.7 29 | 30 | 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-maven-plugin 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/main/java/dk/digitalidentity/Application.java: -------------------------------------------------------------------------------- 1 | package dk.digitalidentity; 2 | 3 | import java.util.List; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.CommandLineRunner; 7 | import org.springframework.boot.SpringApplication; 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; 9 | import org.springframework.ldap.core.LdapTemplate; 10 | import org.springframework.ldap.filter.AndFilter; 11 | import org.springframework.ldap.filter.EqualsFilter; 12 | 13 | import dk.digitalidentity.app.LdapPerson; 14 | import dk.digitalidentity.app.PersonRepo; 15 | 16 | @SpringBootApplication 17 | public class Application implements CommandLineRunner { 18 | 19 | @Autowired 20 | LdapTemplate ldapTemplate; 21 | 22 | public void run(String... args) { 23 | 24 | PersonRepo dao = new PersonRepo(); 25 | dao.setLdapTemplate(ldapTemplate); 26 | 27 | AndFilter andFilter = new AndFilter(); 28 | andFilter.and(new EqualsFilter("objectclass", "person")); 29 | andFilter.and(new EqualsFilter("sAMAccountName", "daniel")); 30 | andFilter.and(new EqualsFilter("memberof", "CN=TestGroup,DC=example,DC=org")); 31 | 32 | List allPerson = dao.getAllPerson(andFilter); 33 | for (LdapPerson p : allPerson) { 34 | System.out.println(p.getCn()); 35 | } 36 | } 37 | 38 | public static void main(String[] args) { 39 | SpringApplication.run(Application.class, args); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/dk/digitalidentity/app/ActiveDirectoryHelper.java: -------------------------------------------------------------------------------- 1 | package dk.digitalidentity.app; 2 | 3 | import javax.naming.directory.DirContext; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.ldap.core.support.LdapContextSource; 7 | import org.springframework.ldap.support.LdapUtils; 8 | import org.springframework.stereotype.Component; 9 | 10 | @Component 11 | public class ActiveDirectoryHelper { 12 | 13 | @Autowired 14 | private LdapContextSource contextSource; 15 | 16 | public ActiveDirectoryHelper() { 17 | } 18 | 19 | public boolean authenticate(String userDn, String credentials) { 20 | DirContext ctx = null; 21 | try { 22 | ctx = contextSource.getContext(userDn, credentials); 23 | return true; 24 | } 25 | catch (Exception e) { 26 | // Context creation failed - authentication did not succeed 27 | System.out.println("login failed, " + e); 28 | return false; 29 | } 30 | finally { 31 | // It is imperative that the created DirContext instance is always 32 | // closed 33 | LdapUtils.closeContext(ctx); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/dk/digitalidentity/app/LdapConfig.java: -------------------------------------------------------------------------------- 1 | package dk.digitalidentity.app; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.ldap.core.LdapTemplate; 6 | import org.springframework.ldap.core.support.LdapContextSource; 7 | 8 | @Configuration 9 | public class LdapConfig { 10 | 11 | @Bean 12 | public LdapContextSource getContextSource() throws Exception{ 13 | LdapContextSource ldapContextSource = new LdapContextSource(); 14 | ldapContextSource.setUrl("ldap://54.154.65.69:389"); 15 | ldapContextSource.setBase("dc=example,dc=org"); 16 | ldapContextSource.setUserDn("daniel@example"); 17 | ldapContextSource.setPassword("Test1234"); 18 | return ldapContextSource; 19 | } 20 | 21 | @Bean 22 | public LdapTemplate ldapTemplate() throws Exception{ 23 | LdapTemplate ldapTemplate = new LdapTemplate(getContextSource()); 24 | ldapTemplate.setIgnorePartialResultException(true); 25 | ldapTemplate.setContextSource(getContextSource()); 26 | return ldapTemplate; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/dk/digitalidentity/app/LdapPerson.java: -------------------------------------------------------------------------------- 1 | package dk.digitalidentity.app; 2 | 3 | public class LdapPerson { 4 | private String cn; 5 | 6 | public String getCn() { 7 | return cn; 8 | } 9 | 10 | public void setCn(String cn) { 11 | this.cn = cn; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/dk/digitalidentity/app/PersonRepo.java: -------------------------------------------------------------------------------- 1 | package dk.digitalidentity.app; 2 | 3 | import java.util.List; 4 | 5 | import javax.naming.NamingException; 6 | import javax.naming.directory.Attributes; 7 | 8 | import org.springframework.ldap.core.AttributesMapper; 9 | import org.springframework.ldap.core.LdapTemplate; 10 | import org.springframework.ldap.filter.Filter; 11 | 12 | public class PersonRepo { 13 | private LdapTemplate ldapTemplate; 14 | 15 | public void setLdapTemplate(LdapTemplate ldapTemplate) { 16 | this.ldapTemplate = ldapTemplate; 17 | } 18 | 19 | public List getAllPerson(Filter filter) { 20 | return ldapTemplate.search("", filter.encode(), new AttributesMapper() { 21 | @Override 22 | public LdapPerson mapFromAttributes(Attributes attr) throws NamingException { 23 | LdapPerson person = new LdapPerson(); 24 | person.setCn((String) attr.get("cn").get()); 25 | return person; 26 | } 27 | }); 28 | } 29 | 30 | } 31 | --------------------------------------------------------------------------------