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配置文件

 

    <bean id="contextSource"  
        class="org.springframework.ldap.core.support.LdapContextSource">  
        <property name="url" value="ldap://192.168.0.22:389" />  
        <property name="base" value="dc=ygsoft,dc=com" />  
        <property name="userDn" value="whuqin@yahoo.com" />  
        <property name="password" value="1234.abcd" />
        <property name="referral" value="follow"></property>
    </bean>
  
    <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">  
        <constructor-arg ref="contextSource" />
    </bean>  
  	
    <bean id="userDao" class="cn.com.ldap.UserDaoLdapImpl">
    	<property name="ldapTemplate">
    		<ref bean="ldapTemplate"/>
    	</property>
    </bean>

 

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

UserDaoLdapImpl关键代码

    private LdapTemplate ldapTemplate;   
    private ContextSource contextSource;
    public void setLdapTemplate(LdapTemplate ldapTemplate) {   
        this.ldapTemplate = ldapTemplate;   
    }   
    public void setContextSource(ContextSource contextSource) {
    	this.contextSource = contextSource;
    }
    public boolean authenticate(String userName, String password) {
    	AndFilter filter = new AndFilter();
  	  	filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("userPrincipalName", userName));
    	// Actual filter will differ depending on LDAP Server and schema
    	List<String> results = ldapTemplate.search("", filter.toString(),
    			new DnContextMapper());
    	if (results.size() != 1) return false;
     
    	DirContext ctx = null;
    	try {
    		ctx = contextSource.getContext(results.get(0), password);
    		return true;
    	} catch (Exception e) {
    		return false;
    	} finally {
    		LdapUtils.closeContext(ctx);
    	}
    }
    private final static class DnContextMapper extends
	AbstractParameterizedContextMapper<String> {
    	@Override
    	protected String doMapFromContext(DirContextOperations ctx) {
    		return ctx.getNameInNamespace();
    	}
    }


这样不管什么情况,都不会在控制台出现异常提示了。

 

转载于:https://www.cnblogs.com/whuqin/archive/2012/04/11/4982048.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值