I had a lot of issues during integrating client LDAP/AD infrastructure with existing spring-boot applications.
While everything was working with Apache Directory Studio or with ldap-utils, Sprig Boot application had problems with authenticating to given services.
I created a library io.github.inyourhead:ldap-util, which provides ability to test LDAP/AD connection in easy way.
It uses existing spring-security features (spring-security-ldap) to ensure that your application will work as expected.
You need:
- Java 17+
- Maven 3.8.4+
- (optional) docker/docker-compose for running ldap tests
- (optional) standalone instance of Windows Server 2016+ to run ad tests
Run in terminal:
./mvnw clean install -DskipTests
To run tests you need to configure docker/docker compose
To run ldap tests type in terminal:
./mvnw clean install -Pldap
To run ad tests type in terminal:
./mvnw clean install -Pad -Dad.http.port=<ad-port> -Dad.hostname=<ad-hostname> -Dad.scheme=<ldap/ldaps>
See Configure AD before testing for more details
After installation step you should have jar file named ldap-util-app.jar
in /ldap-util/app/target/
directory
Run it like shown below:
java -jar -Dspring.profiles.active=ldap,ad ldap-util-app.jar
Application should be available on http://localhost:8080/
Be aware, that <version>
is related to used spring boot version.
Add maven dependency to your application:
<dependency>
<groupId>io.github.inyourhead</groupId>
<artifactId>util</artifactId>
<version>3.4.1</version>
</dependency>
(You can find it here)
And in your spring boot application add:
@Import(LdapUtilAutoConfiguration.class)
public class LdapUtilApplication {
public static void main(String[] args) {
SpringApplication.run(LdapUtilApplication.class, args);
}
}
to allow autowiring
@Service
public class MyUtilService {
@Autowired
AuthService<AdConfig> adAuthService;
@Autowired
AuthService<LdapConfig> ldapAuthService;
public void makeOperation() {
//TODO implement me!
boolean isValidAdUserAndConfig = adAuthService.autenticate(Credentials, AdConfig);
boolean isValidLdapUserAndConfig = ldapAuthService.autenticate(Credentials, LdapConfig);
}
}
Sure. Grab this docker-compose.yml file and run it!
You may also run docker command:
docker run -p 8080:8080 --name ldap-util-app -d inyourhead/ldap-util-app:3.4.1
- add more tests for AD
- export configuration from AD to allow testing on you own AD server
- allow to search in LDAP/AD
- return spring boot configuration associated with given authentication
- change package names according to groupId