2011-03-10 93 views
7

我試圖驗證,然後使用Spring LDAP和Spring安全性查詢我們的公司LDAP。我設法使認證工作,但是當我嘗試運行搜索我總是得到以下異常春季LDAP - 綁定成功連接

In order to perform this operation a successful bind must be completed on the connection

大量的研究後,我有一個理論,之前之後我驗證和我可以查詢我需要綁定連接。我只是不知道什麼和如何?

只要提一下 - 我可以使用JXplorer成功瀏覽和搜索我們的LDAP,所以我的參數是正確的。

這裏是我的securityContext.xml

<security:http auto-config='true'> 
    <security:intercept-url pattern="/reports/goodbye.html" 
      access="ROLE_LOGOUT" /> 
    <security:intercept-url pattern="/reports/**" access="ROLE_USER" /> 
    <security:http-basic /> 
    <security:logout logout-url="/reports/logout" 
      logout-success-url="/reports/goodbye.html" /> 
</security:http> 
<security:ldap-server url="ldap://s140.foo.com:1389/dc=td,dc=foo,dc=com" /> 
<security:authentication-manager> 
    <security:authentication-provider ref="ldapAuthProvider"> 
</security:authentication-provider> 
</security:authentication-manager> 
<!-- Security beans --> 
<bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource"> 
    <constructor-arg value="ldap://s140.foo.com:1389/dc=td,dc=foo,dc=com" /> 
</bean> 
<bean id="ldapAuthProvider" 
    class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider"> 
    <constructor-arg> 
     <bean class="foo.bar.reporting.server.security.ldap.LdapAuthenticatorImpl"> 
      <property name="contextFactory" ref="contextSource" /> 
      <property name="principalPrefix" value="TD\" /> 
      <property name="employee" ref="employee"></property> 
     </bean> 
    </constructor-arg> 
    <constructor-arg> 
     <bean class="foo.bar.reporting.server.security.ldap.LdapAuthoritiesPopulator" /> 
    </constructor-arg> 
</bean> 
<!-- DAOs --> 
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate"> 
    <constructor-arg ref="contextSource" /> 

的部分下面是LdapAuthenticatorImpl代碼片段執行身份驗證。這裏沒有問題:

@Override 
public DirContextOperations authenticate(final Authentication authentication) { 
    // Grab the username and password out of the authentication object. 
    final String name = authentication.getName(); 
    final String principal = this.principalPrefix + name; 
    String password = ""; 
    if (authentication.getCredentials() != null) { 
     password = authentication.getCredentials().toString(); 
    } 
    if (!("".equals(principal.trim())) && !("".equals(password.trim()))) { 
     final InitialLdapContext ldapContext = (InitialLdapContext) 
    this.contextFactory.getContext(principal, password); 
     // We need to pass the context back out, so that the auth provider 
     // can add it to the Authentication object. 
     final DirContextOperations authAdapter = new DirContextAdapter(); 
     authAdapter.addAttributeValue("ldapContext", ldapContext); 
     this.employee.setqId(name); 
     return authAdapter; 
    } else { 
     throw new BadCredentialsException("Blank username and/or password!"); 
    } 
} 

下面是從EmployeeDao另一個代碼片段與我的徒勞嘗試查詢:

public List<Employee> queryEmployeesByName(String query) 
    throws BARServerException { 
    AndFilter filter = new AndFilter(); 
    filter.and(new EqualsFilter("objectclass", "person")); 
    filter.and(new WhitespaceWildcardsFilter("cn", query)); 
    try { 
     // the following line throws bind exception 
     List result = ldapTemplate.search(BASE, filter.encode(), 
      new AttributesMapper() { 
      @Override 
      public Employee mapFromAttributes(Attributes attrs) 
       throws NamingException { 
       Employee emp = new Employee((String) attrs.get("cn").get(), 
        (String) attrs.get("cn").get(), 
         (String) attrs.get("cn").get()); 
       return emp; 
      } 
     }); 
     return result; 
    } catch (Exception e) { 
     throw new BarServerException("Failed to query LDAP", e); 
    } 
} 

最後一點 - 我越來越

org.springframework.ldap.UncategorizedLdapException: 
    Uncategorized exception occured during LDAP processing; nested exception is 
    javax.naming.NamingException: [LDAP: error code 1 - 00000000: LdapErr: 
    DSID-0C090627, comment: In order to perform this operation a successful bind 
    must be completed on the connection., data 0, vece]; remaining name 
    'DC=TD,DC=FOO,DC=COM' 
+0

我知道這是舊的,但@Bostone你能幫我解決這個問題。我得到完全相同的異常,但是我在用戶首次輸入憑據的登錄頁面上收到此錯誤。當輸入正確的用戶名和密碼時,ldap會成功返回,但出現以下錯誤:[LDAP:錯誤代碼1- 00000000:LdapErr:DSID-0C090627,註釋:爲了執行此操作,必須在連接上完成成功綁定。 ,數據0,vece];重新命名「' – user1647708 2014-03-25 16:48:39

+0

@ user1647708請參閱下面的答案。它爲我工作 – Bostone 2014-03-26 17:29:53

回答

4

它看起來異常就像你的LDAP被配置爲不允許搜索而不綁定它(沒有匿名綁定)。您還實施了PasswordComparisonAuthenticator而不是BindAuthenticatorauthenticate到LDAP。

您可以嘗試修改您的queryEmployeesByName()方法進行綁定,然後搜索,查看doc中的一些示例。

+0

這聽起來像一個評論,你有什麼建議?我知道這是可能的,因爲我可以使用JXplorer(這是Java應用程序)來連接和搜索LDAP我只是無法弄清楚如何綁定。我想我過去曾嘗試過BindAuthenticator,並且以相同的例外 – Bostone 2011-03-10 05:09:42

+0

@ Droidln.net結束。用明確的建議編輯答案。 – Raghuram 2011-03-10 10:06:17

+0

這是我用來編碼驗證的非常文檔。當他們在說綁定/解除綁定時,他們的意思是插入/刪除特定示例中的記錄 – Bostone 2011-03-10 18:13:02

3

我打算接受@Raghuram答案,主要是因爲它讓我思考着正確的方向。

爲什麼我的代碼失敗了?原來 - 我接線的方式我試圖進行匿名搜索,這是系統禁止的 - 因此錯誤。

如何重新連接上面的示例工作?第一件事(也就是醜陋的事情),你需要提供用戶名和用於訪問系統的用戶密碼。甚至在您登錄和認證時也是非常不直觀的,即使您正在使用BindAuthenticator系統也不會嘗試重用您的憑證。遊民。所以,你需要堅持2個參數爲contextSource定義,像這樣:

<bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource"> 
    <constructor-arg value="ldap://foo.com:389/dc=td,dc=foo,dc=com" /> 
    <!-- TODO - need to hide this or encrypt a password --> 
    <property name="userDn" value="CN=admin,OU=Application,DC=TD,DC=FOO,DC=COM" /> 
    <property name="password" value="blah" /> 
</bean> 

這樣做,讓我與通用BindAuthenticator,然後我的Java搜索開始着手

+1

您是否已將「userDn」和「password」的值設置爲實際值?我的意思是,如果我這樣做,每個人都會以純文本的方式看到它。我怎樣才能從登錄表單讀取它? – 2017-05-11 14:08:05

0

我得到了同樣的錯誤取代認證的自定義實現,找不到解決方案。 最後,我將應用程序池標識更改爲網絡服務,並且所有操作都像魅力一樣。 (我有Windows身份驗證和匿名在我的網站上啓用)