DEV Community

Al-Karid
Al-Karid

Posted on

Authenticating a Spring Boot Application with Active Directory

Authentication with Active Directory in a Spring Boot application is straightforward and requires minimal setup. You don't need to install an embedded server for testing connectivity or additional complex packages. In fact, the process is streamlined with just a couple of dependencies and a simple configuration.

Dependencies

First, ensure you have the necessary dependencies in your pom.xml or build.gradle:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-ldap</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Configuration

Next, configure your Spring Security in a WebSecurityConfig class:

@EnableWebSecurity
public class WebSecurityConfig{

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests((authorize) -> authorize.anyRequest().fullyAuthenticated())
            .formLogin(Customizer.withDefaults());

        return http.build();

    @Bean
    public ActiveDirectoryLdapAuthenticationProvider authenticationProvider() {
        return new ActiveDirectoryLdapAuthenticationProvider("domain.com", "ldap://ip-addr");
    }
}

Enter fullscreen mode Exit fullscreen mode

And that's it, happy securing ๐Ÿ˜Ž !!

Top comments (0)