集成spring-ldap

1.pom文件增加

  <dependency>
            <groupId>org.springframework.ldap</groupId>
            <artifactId>spring-ldap-core</artifactId>
            <version>2.3.2.RELEASE</version>
        </dependency>

2.spring配置文件增加

<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.xsd
      http://www.springframework.org/schema/ldap
http://www.springframework.org/schema/ldap/spring-ldap.xsd"
>
    <bean id="contextSource"    
        class="org.springframework.ldap.core.support.LdapContextSource">    
        <property name="url" value="ldap://192.168.80.123:389" />    
        <property name="base" value="dc=kpzc,dc=local" />    
        <property name="userDn" value="admin@kpzc.local" />    
        <property name="password" value="kpzc" />  
    </bean>  
      
    <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">    
        <constructor-arg ref="contextSource" />  
    </bean>

url 账号 密码 我是固定写死的 也可配置在properties文件中

3.启动项目会出现


17:02:24.853 [main] DEBUG o.s.l.c.s.AbstractContextSource - AuthenticationSource not set - using default implementation
17:02:24.854 [main] DEBUG o.s.l.c.s.AbstractContextSource - Not using LDAP pooling
17:02:24.857 [main] DEBUG o.s.l.c.s.AbstractContextSource - Trying provider Urls: ldap://192.168.80.123:389/dc=kpzc,dc=local

即可使用Autowired注解了

@Autowired
private LdapTemplate ldapTemplate;
原文地址:https://www.cnblogs.com/zjk1/p/9438152.html