spring ldap demo

tag: spring ldap demo

最近在弄AD 活动目录的登录功能,用到了spring-ldap1.3,留个脚印!

package sample;
import java.util.List;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import org.springframework.ldap.core.AttributesMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.ldap.filter.WhitespaceWildcardsFilter;
import sample.bean.Person;

public class SpringLdapDemo {

	protected static final Log log = LogFactory.getLog(SpringLdapDemo.class);

	public static void main(String[] args) {
		SpringLdapDemo sld=new SpringLdapDemo();
		AbstractApplicationContext lbf = 
			new ClassPathXmlApplicationContext("/applicationContext-ldap.xml");
		lbf.registerShutdownHook();
		GenericBeanFactoryAccessor gbfa = new GenericBeanFactoryAccessor(lbf);
		LdapTemplate lt = gbfa.getBean("ldapTemplate");


		List<?> usersList =sld.getAllPersonNames(lt);
		//打印出用户集合
		log.info("打印出用户集合");
		log.info(usersList);
		log.info(usersList.size());

		log.info("用户查询");
		List findlist=sld.getPersonNamesByLastName("jgec", lt);
		log.info(findlist);
	}
	//获取用户名
	public List<?> getAllPersonNames(LdapTemplate lt) {
		return lt.search(
				"", "(objectclass=person)",
				new AttributesMapper() {
					public Object mapFromAttributes(Attributes attrs)
					throws NamingException {
						return attrs.get("cn").get();
					}
				});
	}
	//获取用户dn
	public List getPersonNamesByLastName(String lastName,LdapTemplate lt) {
		AndFilter filter = new AndFilter();
		filter.and(new EqualsFilter("objectclass", "person"));
		filter.and(new WhitespaceWildcardsFilter("cn", lastName));
		return lt.search(
				"", filter.encode(),
				new AttributesMapper() {
					public Object mapFromAttributes(Attributes attrs)
					throws NamingException {
						return attrs.get("distinguishedName").get();
					}
				});
	}

	public List getAllPersons(LdapTemplate lt) {
		return lt.search(
				"", "(objectclass=person)", new PersonAttributesMapper());
	}
	public sample.bean.Person getPersonByDn(String dn,LdapTemplate lt) {
		return (Person)lt.lookup(dn, new PersonAttributesMapper());
	}

	private class PersonAttributesMapper implements AttributesMapper {

		public Object mapFromAttributes(Attributes attrs) throws NamingException {
			Person person = new Person();
			person.setCn((String) attrs.get("cn").get());
			person.setSn((String) attrs.get("sn").get());
			return person;
		}

	}
}

applicationContext-ldap.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
			http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

	<bean id="ldapContextSource" class="org.springframework.ldap.core.support.LdapContextSource">
		
		<property name="referral" value="follow"></property>
		<property name="url" value="ldap://172.16.23.216:389" />
		<property name="base" value="DC=chy,DC=com,DC=cn" />
		<property name="userDn"
			value="CN=Administrator,CN=Users,DC=chy,DC=com,DC=cn" />
		<property name="password" value="1234" />
		<property name="baseEnvironmentProperties">
			<map>
				<entry key="java.naming.security.authentication" value="simple" />
			</map>
		</property>
	</bean>
	<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
		<property name="contextSource" ref="ldapContextSource" />
	</bean>
</beans>


public class Person {
String cn;
String sn;
public String getCn() {
	return cn;
}
public void setCn(String cn) {
	this.cn = cn;
}
public String getSn() {
	return sn;
}
public void setSn(String sn) {
	this.sn = sn;
}
public Person(String cn, String sn) {
	super();
	this.cn = cn;
	this.sn = sn;
}
public Person() {
}
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值