Spring Ldap开发示例

LDAP服务器搭建教程:http://jingyan.baidu.com/article/1709ad80616d864634c4f0b7.html

pdm.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.demo</groupId>
  <artifactId>Test1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context</artifactId>
	    <version>4.1.3.RELEASE</version>
	</dependency>
	  <dependency>
	        <groupId>org.springframework.ldap</groupId>
	        <artifactId>spring-ldap-core</artifactId>
	        <version>2.3.1.RELEASE</version>
	    </dependency>
	    <dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-expression</artifactId>
		    <version>4.3.11.RELEASE</version>
		</dependency>
    </dependencies>
</project>
PersonDemo.java

package com.demo.repo;

import static org.springframework.ldap.query.LdapQueryBuilder.query;

import java.util.List;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;

import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;

import com.hcy.pojo.Person;

public class PersonDemo {
	private LdapTemplate ldapTemplate;
	
	public void setLdapTemplate(LdapTemplate ldapTemplate) {
		this.ldapTemplate = ldapTemplate;
	}
	
	//使用AttributesMapper返回单个属性
	public List<String> getAllPersonNames() {
		System.out.println("in");
		return ldapTemplate.search(query().where("objectclass").is("person"), new AttributesMapper<String>() {
			//方法是接口中定义的,我们来实现
			public String mapFromAttributes(Attributes attr) {
				System.out.println(String.valueOf(attr));
				try {
					return (String) attr.get("cn").get();
				} catch (NamingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
			}
		});
	}
	//使用AttributeMapper返回Person对象
	public List<Person> getAllPersons() {
		return ldapTemplate.search(query().where("objectclass").is("person"), new AttributesMapper<Person>() {
			public Person mapFromAttributes(Attributes attr) throws NamingException {
				Person person = new Person();
				person.setFullName((String)attr.get("cn").get());
				person.setSn((String)attr.get("sn").get());
				System.out.println(person.toString());
				return person;
			}});
	}
}

Person.java

package com.demo.pojo;


import javax.naming.Name;

import org.springframework.ldap.odm.annotations.Entry;

public class Person {
	private Name dn;
	
	private String sn;
	
	private String fullName;
	
	private String description;

	public Name getDn() {
		return dn;
	}

	public void setDn(Name dn) {
		this.dn = dn;
	}
	
	public String getSn() {
		return sn;
	}

	public void setSn(String sn) {
		this.sn = sn;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public String toString() {
		return "Person [dn=" + dn + ", sn=" + sn + ", fullName=" + fullName + ", description=" + description + "]";
	}
	
}
Test.java

package com.demo.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hcy.repo.PersonDemo;

public class Test {
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:springldap.xml");
        PersonDemo personDemo=(PersonDemo)ac.getBean("personDemo");  
        //使用AttributesMapper返回单个属性
        //personDemo.getAllPersonNames();
        
        //使用AttributesMapper返回Person对象
        personDemo.getAllPersons();
    }


}

springldap.xml

<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">
       <!-- 配置contextSource -->
       <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
	       	<property name="url" value="ldap://172.16.4.78:389" />
	       	<property name="base" value="dc=baidu, dc=cn" />
	       	<property name="userDn" value="cn=admin,dc=baidu, dc=cn" />
	       	<property name="password" value="123456" />
       </bean>
       <!-- 配置LdapTemplate -->
	    <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
	        <constructor-arg ref="contextSource" />
	    </bean>
       <!-- 配置实体类 -->
	    <bean id="personDemo" class="com.demo.repo.PersonDemo">
	        <property name="ldapTemplate" ref="ldapTemplate" />
	    </bean>
</beans>





猜你喜欢

转载自blog.csdn.net/qq_22156459/article/details/78054895