Spring Boot · 2026-03-03

Spring Boot와 LDAP로 인증·권한 관리하기

Spring Boot와 LDAP 연동으로 인증과 권한 관리를 단계별로 정리한다. application.yml 설정, Java 보안 설정, 권한 매핑과 테스트를 포함한 상세한 설정

작성일 : 2026-03-03 ㆍ 작성자 : 관리자
post
목차

개요

기업 환경에서 LDAP는 중앙 사용자 디렉터리로 널리 사용된다. Spring Boot와 연동하면 인증과 권한 관리를 일원화할 수 있다. 이 글은 spring boot ldap 인증을 처음 접하는 개발자도 이해할 수 있도록 기본 개념부터 설정 예제, 주의사항까지 순서대로 설명한다.

사전 준비

시작 전에 다음 항목을 준비한다.

  • Spring Boot 2.x 이상 프로젝트
  • LDAP 서버 (예: OpenLDAP, Active Directory)
  • spring-boot-starter-security, spring-boot-starter-ldap 의존성

pom.xml 의존성

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
  </dependency>
  <dependency> 
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-ldap</artifactId>
  </dependency>
</dependencies>

application.yml 설정

LDAP 연결 정보와 사용자/그룹 검색 기준을 application.yml에 설정한다. spring boot ldap 설정을 명확히 하면 보안 구성과 매핑이 수월해진다.

spring:
  ldap:
    urls: ldap://ldap.example.com:389
    base: dc=example,dc=com
    username: cn=admin,dc=example,dc=com
    password: adminpassword
  security:
    ldap:
      user-dn-pattern: cn={0},ou=people
      group-search-base: ou=groups
      group-search-filter: (member={0})

Spring Security 설정

LDAP 인증을 Spring Security와 연동하려면 SecurityFilterChain 또는 WebSecurityConfigurerAdapter로 설정한다. 최신 버전에서는 SecurityFilterChain 빈을 추천한다.

예제: SecurityConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.ldap.DefaultSpringSecurityContextSource;

@Configuration
public class SecurityConfig {

  @Bean
  public DefaultSpringSecurityContextSource contextSource() {
    return new DefaultSpringSecurityContextSource(
      java.util.List.of("ldap://ldap.example.com:389"), "dc=example,dc=com");
  }

  @Bean
  public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http
      .authorizeRequests(a -> a
        .antMatchers("/admin/**").hasRole("ADMIN")
        .anyRequest().authenticated()
      )
      .ldapAuthentication(la -> la
        .userDnPatterns("uid={0},ou=people")
        .groupSearchBase("ou=groups")
        .contextSource(contextSource())
      )
      .formLogin();
    return http.build();
  }
}

권한 매핑과 그룹 처리

LDAP에서는 권한을 그룹으로 관리하는 경우가 많다. group-search-filter를 사용해 사용자가 속한 그룹을 가져오고, 필요한 경우 추가 매핑을 한다. 예를 들어 LDAP 그룹명과 애플리케이션 역할을 매핑하려면 GrantedAuthoritiesMapper 또는 custom LdapAuthoritiesPopulator를 구현한다.

예제: 간단한 권한 매핑

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator;

public class SimpleAuthoritiesPopulator implements LdapAuthoritiesPopulator {
  @Override
  public Collection<GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) {
    // LDAP 그룹 정보를 읽어 애플리케이션 권한으로 변환
    Collection<GrantedAuthority> authorities = new ArrayList<>();
    // 예: 그룹명이 "developers"이면 ROLE_DEVELOPER 부여
    authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
    return authorities;
  }
}

테스트 방법

  • LDAP 툴(ldapsearch, Apache Directory Studio)로 연결 정보와 검색 필터를 확인
  • 애플리케이션에서 로그 레벨을 DEBUG로 올려 바인딩 및 검색 쿼리 확인
  • 정상 사용자로 로그인해 권한 기반 접근 제어 검증

실무에서의 고려사항

  • 보안: LDAP 바인딩 정보는 외부에 노출되지 않도록 환경변수 또는 Vault에 보관
  • 성능: 그룹 조회가 빈번하면 캐싱 적용을 검토
  • 필터 튜닝: 사용자 검색 기준(user-search-filter)과 그룹 검색 필터 최적화
  • Active Directory 특성: AD의 경우 userPrincipalName, sAMAccountName 등을 상황에 맞게 사용

문제 발생 시 점검 항목

  • 기본 DN(base)과 사용자 DN 패턴 일치 여부
  • 바인드 계정의 조회 권한 여부
  • 검색 필터의 괄호 및 속성 이름 오타
  • TLS/SSL 설정 필요 여부와 포트(636) 확인

마무리

이 글에서는 spring boot ldap 인증을 위한 기본 설정과 ldap spring security 연동 방법, 그리고 spring boot ldap 설정 시 고려할 점을 정리했다. LDAP 구조와 요구사항에 따라 검색 기준과 권한 매핑을 조정하면 안정적인 인증·권한 관리 체계를 구축할 수 있다.

spring boot ldap 인증 ldap spring security 연동 spring boot ldap 설정 Spring Security LDAP 설정 LDAP 권한 매핑 application.yml LDAP 테스트