DEV Community

Al-Karid
Al-Karid

Posted on

3

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 😎 !!

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay