zoukankan      html  css  js  c++  java
  • spring ldap demo

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

    [java] view plaincopy
    1. package sample;  
    2. import java.util.List;  
    3. import javax.naming.NamingException;  
    4. import javax.naming.directory.Attributes;  
    5. import org.springframework.ldap.core.AttributesMapper;  
    6. import org.apache.commons.logging.Log;  
    7. import org.apache.commons.logging.LogFactory;  
    8. import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;  
    9. import org.springframework.context.support.AbstractApplicationContext;  
    10. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    11. import org.springframework.ldap.core.LdapTemplate;  
    12. import org.springframework.ldap.filter.AndFilter;  
    13. import org.springframework.ldap.filter.EqualsFilter;  
    14. import org.springframework.ldap.filter.WhitespaceWildcardsFilter;  
    15. import sample.bean.Person;  
    16.   
    17. public class SpringLdapDemo {  
    18.   
    19.     protected static final Log log = LogFactory.getLog(SpringLdapDemo.class);  
    20.   
    21.     public static void main(String[] args) {  
    22.         SpringLdapDemo sld=new SpringLdapDemo();  
    23.         AbstractApplicationContext lbf =   
    24.             new ClassPathXmlApplicationContext("/applicationContext-ldap.xml");  
    25.         lbf.registerShutdownHook();  
    26.         GenericBeanFactoryAccessor gbfa = new GenericBeanFactoryAccessor(lbf);  
    27.         LdapTemplate lt = gbfa.getBean("ldapTemplate");  
    28.   
    29.   
    30.         List<?> usersList =sld.getAllPersonNames(lt);  
    31.         //打印出用户集合  
    32.         log.info("打印出用户集合");  
    33.         log.info(usersList);  
    34.         log.info(usersList.size());  
    35.   
    36.         log.info("用户查询");  
    37.         List findlist=sld.getPersonNamesByLastName("jgec", lt);  
    38.         log.info(findlist);  
    39.     }  
    40.     //获取用户名  
    41.     public List<?> getAllPersonNames(LdapTemplate lt) {  
    42.         return lt.search(  
    43.                 """(objectclass=person)",  
    44.                 new AttributesMapper() {  
    45.                     public Object mapFromAttributes(Attributes attrs)  
    46.                     throws NamingException {  
    47.                         return attrs.get("cn").get();  
    48.                     }  
    49.                 });  
    50.     }  
    51.     //获取用户dn  
    52.     public List getPersonNamesByLastName(String lastName,LdapTemplate lt) {  
    53.         AndFilter filter = new AndFilter();  
    54.         filter.and(new EqualsFilter("objectclass""person"));  
    55.         filter.and(new WhitespaceWildcardsFilter("cn", lastName));  
    56.         return lt.search(  
    57.                 "", filter.encode(),  
    58.                 new AttributesMapper() {  
    59.                     public Object mapFromAttributes(Attributes attrs)  
    60.                     throws NamingException {  
    61.                         return attrs.get("distinguishedName").get();  
    62.                     }  
    63.                 });  
    64.     }  
    65.   
    66.     public List getAllPersons(LdapTemplate lt) {  
    67.         return lt.search(  
    68.                 """(objectclass=person)"new PersonAttributesMapper());  
    69.     }  
    70.     public sample.bean.Person getPersonByDn(String dn,LdapTemplate lt) {  
    71.         return (Person)lt.lookup(dn, new PersonAttributesMapper());  
    72.     }  
    73.   
    74.     private class PersonAttributesMapper implements AttributesMapper {  
    75.   
    76.         public Object mapFromAttributes(Attributes attrs) throws NamingException {  
    77.             Person person = new Person();  
    78.             person.setCn((String) attrs.get("cn").get());  
    79.             person.setSn((String) attrs.get("sn").get());  
    80.             return person;  
    81.         }  
    82.   
    83.     }  
    84. }  

    applicationContext-ldap.xml


    [html] view plaincopy
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
    4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
    5.         http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
    6.             http://www.springframework.org/schema/aop   
    7.         http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
    8.   
    9.     <bean id="ldapContextSource" class="org.springframework.ldap.core.support.LdapContextSource">  
    10.           
    11.         <property name="referral" value="follow"></property>  
    12.         <property name="url" value="ldap://172.16.23.216:389" />  
    13.         <property name="base" value="DC=chy,DC=com,DC=cn" />  
    14.         <property name="userDn"  
    15.             value="CN=Administrator,CN=Users,DC=chy,DC=com,DC=cn" />  
    16.         <property name="password" value="1234" />  
    17.         <property name="baseEnvironmentProperties">  
    18.             <map>  
    19.                 <entry key="java.naming.security.authentication" value="simple" />  
    20.             </map>  
    21.         </property>  
    22.     </bean>  
    23.     <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">  
    24.         <property name="contextSource" ref="ldapContextSource" />  
    25.     </bean>  
    26. </beans>  


    [java] view plaincopy
    1. public class Person {  
    2. String cn;  
    3. String sn;  
    4. public String getCn() {  
    5.     return cn;  
    6. }  
    7. public void setCn(String cn) {  
    8.     this.cn = cn;  
    9. }  
    10. public String getSn() {  
    11.     return sn;  
    12. }  
    13. public void setSn(String sn) {  
    14.     this.sn = sn;  
    15. }  
    16. public Person(String cn, String sn) {  
    17.     super();  
    18.     this.cn = cn;  
    19.     this.sn = sn;  
    20. }  
    21. public Person() {  
    22. }  
    23. }  
  • 相关阅读:
    TJU_SCS_软件测试_lab2_Selenium
    TJU_SCS_软件测试_homework3
    阅读《基于谱聚类的终端区飞行轨迹分析》笔记
    阅读《基于转弯点聚类的航空飞行轨迹分析》笔记
    TJU_SCS_软件测试_Lab1
    TJU_SCS_软件测试_homework2
    TJU_SCS_软件测试_homework1——《error impressed me most》
    TJU_SCS_C#学习笔记(10)
    TJU_SCS_C#学习笔记(9)
    TJU_SCS_C#学习笔记(8)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318067.html
Copyright © 2011-2022 走看看