您的位置:首页 > 编程语言 > Java开发

spring ldap demo

2015-06-15 14:35 316 查看
最近在弄AD 活动目录的登录功能,用到了spring-ldap1.3,留个脚印!

[java] view
plaincopy

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

[html] view
plaincopy

<?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>

[java] view
plaincopy

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() {

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: