Last active
January 11, 2019 06:29
-
-
Save wytten/10670050 to your computer and use it in GitHub Desktop.
Simple Active Directory authentication with Spring LDAP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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