zoukankan      html  css  js  c++  java
  • LDAP用户验证(Spring-LDAP)

    LDAP-Lightweight Directory Access Protocol。LDAP服务器可以是任何一个开源或商用的LDAP目录服务器,而客户端都可以使用同样的协议、客户端连接软件包和查询命令与LDAP服务器进行交互。

    LDAP目录是树形结构,目录有条目组成。条目是具有区别名DN(Distinguished Name)的属性(Attribute)集合,条目相当于表,DN相当于关系数据库表中的关键字(Primary Key),属性由类型(Type)和多个值(Values)组成。

    DN-Distinguished Name,区别名,具有唯一性;DC-District,所属区域;OU-Organization Unit,所属组织;CN/UID-Common Name/Unique ID 名字。

    如下图,uid-tsyroid的DN就是cn=tsyroid,ou=people,dc=syroidmanor,dc=com

    本文使用Spring-LDAP进行用户验证,下载了1.3.1版本

    applicationContex.xml配置文件

    [html] view plaincopy
    1. <bean id="contextSource"    
    2.     class="org.springframework.ldap.core.support.LdapContextSource">    
    3.     <property name="url" value="ldap://192.168.0.22:389" />    
    4.     <property name="base" value="dc=ygsoft,dc=com" />    
    5.     <property name="userDn" value="whuqin@yahoo.com" />    
    6.     <property name="password" value="1234.abcd" />  
    7.     <property name="referral" value="follow"></property>  
    8. </bean>  
    9.   
    10. <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">    
    11.     <constructor-arg ref="contextSource" />  
    12. </bean>    
    13.   
    14. <bean id="userDao" class="cn.com.ldap.UserDaoLdapImpl">  
    15.     <property name="ldapTemplate">  
    16.         <ref bean="ldapTemplate"/>  
    17.     </property>  
    18. </bean>  

    userDn是用户区别名,格式应该为cn=xxx,ou=xxx,dc=xxx。而本文是由于公司LDAP服务器设置,使用用户的userPrincipalName进行唯一标示。注意referral要设置为follow,否则会出现异常“Unprocessed Continuation Reference(s);”,设置为follow的意思好像是自动接下处理。。。。

    UserDaoLdapImpl关键代码

    [java] view plaincopy
    1.    private LdapTemplate ldapTemplate;     
    2.    private ContextSource contextSource;  
    3.    public void setLdapTemplate(LdapTemplate ldapTemplate) {     
    4.        this.ldapTemplate = ldapTemplate;     
    5.    }     
    6.    public void setContextSource(ContextSource contextSource) {  
    7.     this.contextSource = contextSource;  
    8.    }  
    9.    public boolean authenticate(String userName, String password) {  
    10.     AndFilter filter = new AndFilter();  
    11.         filter.and(new EqualsFilter("objectclass""person")).and(new EqualsFilter("userPrincipalName", userName));  
    12.     // Actual filter will differ depending on LDAP Server and schema  
    13.     List<String> results = ldapTemplate.search("", filter.toString(),  
    14.             new DnContextMapper());  
    15.     if (results.size() != 1return false;  
    16.       
    17.     DirContext ctx = null;  
    18.     try {  
    19.         ctx = contextSource.getContext(results.get(0), password);  
    20.         return true;  
    21.     } catch (Exception e) {  
    22.         return false;  
    23.     } finally {  
    24.         LdapUtils.closeContext(ctx);  
    25.     }  
    26.    }  
    27.    private final static class DnContextMapper extends  
    28. AbstractParameterizedContextMapper<String> {  
    29.     @Override  
    30.     protected String doMapFromContext(DirContextOperations ctx) {  
    31.         return ctx.getNameInNamespace();  
    32.     }  
    33.    }  

    这样不管什么情况,都不会在控制台出现异常提示了。
  • 相关阅读:
    The Quad
    将OrCAD Capture CIS的设计文件(.dsn)导入到PADS Logic VX.2.3
    OrCAD Capture CIS 16.6 将版本16.6的设计文件另存为版本16.2的设计文件
    Eclipse IDE 添加jar包到Java工程中
    PADS Logic VX.2.3 修改软件界面语言
    切换Allegro PCB Editor
    Allegro PCB Design GXL (legacy) 将brd文件另存为低版本文件
    Allegro PCB Design GXL (legacy) 设置自动保存brd文件
    Could not create an acl object: Role '16'
    windows 下apache开启FastCGI
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318069.html
Copyright © 2011-2022 走看看