2012-08-28 89 views
1

我得到以下異常而認證用戶:春LDAP:InvalidNameException:/:[LDAP:錯誤代碼34

如果我用值的applicationContext這樣的:

<property name="url" value="ldap://10.10.10.10:389/DC=lab2,DC=ins" /> 
<property name="base" value="DC=lab2,DC=ins" /> 
<property name="userDn" value="CN=Ldap Bind,OU=Service Accounts,OU=TECH,DC=lab2,DC=ins" /> 

例外是:

Exception in thread "main" org.springframework.ldap.InvalidNameException: /: [LDAP: error code 34 - 0000208F: NameErr: DSID-031001BA, problem 2006 (BAD_NAME), data 8349, best match of: 
    'DC=lab2,DC=ins/dc=lab2,dc=ins' 

否則,如果應用程序上下文是這樣的:

<property name="url" value="ldap://10.10.10.10:389" /> 
<property name="base" value="DC=lab2,DC=ins" /> 
<property name="userDn" value="CN=Ldap Bind,OU=Service Accounts,OU=TECH,DC=lab2,DC=ins" /> 

異常將是:

Exception in thread "main" org.springframework.ldap.PartialResultException: nested exception is javax.naming.PartialResultException [Root exception is javax.naming.CommunicationException: lab2.ins:389 [Root exception is java.net.UnknownHostException: lab2.ins]] 
    at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:205) 

身份驗證方法:

public boolean authenticate(String userName, String password) { 
    AndFilter filter = new AndFilter(); 
    filter.and(new EqualsFilter("objectclass", "person")).and(
       new EqualsFilter("sAMAccountName", userName)); 
    return ldapTemplate.authenticate(DistinguishedName.EMPTY_PATH, filter 
       .toString(), password); 
} 

的applicationContext.xml

<bean id="contextSource" 
     class="org.springframework.ldap.core.support.LdapContextSource"> 
    <property name="url" value="ldap://10.10.10.10:389" /> 
    <property name="base" value="DC=lab2,DC=ins" /> 
    <property name="userDn" value="CN=Ldap Bind,OU=Service Accounts,OU=TECH,DC=lab2,DC=ins" /> 
    <property name="password" value="secret" /> 
    <property name="baseEnvironmentProperties"> 
     <map> 
      <entry key="java.naming.referral"> 
       <value>follow</value> 
      </entry> 
     </map> 
    </property> 
</bean> 
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate"> 
    <constructor-arg ref="contextSource" /> 
</bean> 
<bean id="ldapContact" 
     class="ldap.ContactLDAP "> 
    <property name="ldapTemplate" ref="ldapTemplate" /> 
</bean> 

識別TestClass:

Resource r = new ClassPathResource("applicationContext.xml"); 
BeanFactory factory = new XmlBeanFactory(r); 
ContactLDAP contact = (ContactLDAP) factory.getBean("ldapContact"); 

System.out.println(contact.authenticate("username", "secret")); 

我在這裏錯過了什麼?

回答

0

在專有名稱中有一個斜槓/字符。雖然這是DN中的合法字符,但也許它應該是逗號,。也Distinguished Names

+0

我已經添加了基地ldap網址,這就是爲什麼它來了,但刪除後,我收到了一些其他異常。 –

1

發現你並不需要

<property name="base" value="DC=lab2,DC=ins" /> 

如用戶DN,你已經把完整DN。

<bean id="contextSource" 
      class="org.springframework.ldap.core.support.LdapContextSource"> 
      <property name="url" value="ldap://10.10.10.10:389" /> 
      <property name="userDn" value="CN=Ldap Bind,OU=Service Accounts,OU=TECH,DC=lab2,DC=ins" /> 
      <property name="password" value="secret" /> 

... 

這應該有效。 (但我會避免在DN中的空格)

相關問題