2010-09-24 92 views
5

我正在嘗試使用spring-security與我設置的本地ADAM實例進行交談。使用LDAP針對使用彈簧安全性的ADAM進行身份驗證

我已經成功安裝了ADAM和設置如下....

  • 實例在本地主機上運行:389
  • 根是O=Company
    • 稱爲OU=Company Users(orgnizationalUnit)孩子
      • 稱爲CN=Mike Q的花生(用戶)
      • uid = mikepassword = welcome

然後我有安裝彈簧安全(版本3.0.3,彈簧框架3.0.4和彈簧LDAP 1.3.0)。春天文件

<security:ldap-server id="contextSource" url="ldap://localhost:389/o=Company"/> 

    <security:authentication-manager> 
    <security:ldap-authentication-provider user-dn-pattern="uid={0},ou=Company Users"/> 
    </security:authentication-manager> 

    <bean class="com.xxx.test.TestAuthentication" lazy-init="false"/> 

而且TestAuthentication

public class TestAuthentication 
{ 
    @Autowired 
    private AuthenticationManager authenticationManager; 

    public void initialise() 
    { 
     Authentication authentication = new UsernamePasswordAuthenticationToken("mike", "welcome"); 
     Authentication reponseAuthentication = authenticationManager.authenticate(authentication); 
    } 
} 

運行此我得到以下錯誤

Caused by: javax.naming.AuthenticationException: [LDAP: error code 49 - 8009030C: LdapErr: DSID-0C090336, comment: AcceptSecurityContext error, data 2030, vece] 
at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3041) 
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2987) 
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2789) 
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2703) 
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:293) 
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:175) 
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:193) 
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:136) 
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:66) 
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667) 
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) 
at javax.naming.InitialContext.init(InitialContext.java:223) 
at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:134) 
at org.springframework.ldap.core.support.LdapContextSource.getDirContextInstance(LdapContextSource.java:43) 
at org.springframework.ldap.core.support.AbstractContextSource.createContext(AbstractContextSource.java:254) 

如果有人能指出我要去哪裏錯了,我會很感激。在這一點上,我只想使用LDAP來驗證輸入的用戶/密碼,沒有比這更復雜的了。

我也對一些一般觀點感興趣,因爲這是我第一次進入LDAP世界。

  • LDAP是否區分大小寫?
  • 是否最好避免空格?
  • 什麼是避免在LDAP查詢中以明文形式發送密碼的一般用例/最佳實踐?

回答

4

好的,所以我花了很多時間解決這個問題。

錯誤代碼2030表示用戶的DN無效。

經過一些試驗和錯誤這裏是一個配置工作,並確保用戶正確搜索。 (你可以使用安全命名空間來重寫這個,但是當我正在處理這個時,使用原始bean定義更清晰)。

<bean id="contextSource" 
     class="org.springframework.security.ldap.DefaultSpringSecurityContextSource"> 
    <constructor-arg value="ldap://localhost:389/cn=Sandbox,dc=ITOrg"/> 
    <property name="userDn" value="cn=superuser,cn=People,cn=Sandbox,dc=ITOrg"/> 
    <property name="password" value="xxxxxx"/> 
    </bean> 

    <bean id="ldapAuthProvider" 
     class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider"> 
    <constructor-arg> 
     <bean class="org.springframework.security.ldap.authentication.BindAuthenticator"> 
     <constructor-arg ref="contextSource"/> 
     <property name="userDnPatterns"> 
      <list> 
      <value>cn={0},cn=People</value> 
      </list> 
     </property> 
     </bean> 
    </constructor-arg> 
    </bean> 

    <bean id="userSearch" class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch"> 
    <constructor-arg index="0" value="cn=People"/> 
    <constructor-arg index="1" value="(cn={0})"/> 
    <constructor-arg index="2" ref="contextSource"/> 
    </bean> 

關鍵的事情是

<property name="userDn" value="cn=superuser,cn=People,cn=Sandbox,dc=ITOrg"/> 

當上下文信息源,它必須是完整DN(它不只是追加它做的URL提供的基地指定用戶DN(ARG構造)。

當使用BindAuthentication

<value>cn={0},cn=People</value> 

此值是在上下文源的的baseDn的頂部上的後綴。

在配置UserSearch

<constructor-arg index="0" value="cn=People"/> 
    <constructor-arg index="1" value="(cn={0})"/> 

我無法得到它與cn=People第二Arg爲但這似乎做工精細工作。請注意,您可以使用用戶的屬性,例如(uid={0})

下面是使用bean定義一些示例代碼...

@Autowired 
    private LdapUserSearch ldapUserSearch; 

    @Autowired 
    private AuthenticationProvider authenticationProvider; 

    public void initialise() 
    { 
     DirContextOperations dirContextOperations = ldapUserSearch.searchForUser("username"); 

     Authentication authentication = authenticationProvider.authenticate(new UsernamePasswordAuthenticationToken("username", "password"));  
    } 

其他一些隨機花絮...

Error 52b - Invalid password 


[LDAP: error code 32 - 0000208D: NameErr: DSID-031521D2, problem 2001 (NO_OBJECT), data 0, best match of: 'CN=Sandbox,DC=ITOrg' 
    - This means the user is not in the administrator role (probably) 

希望這一切可以幫助其他人。

0

我通過添加您嘗試用作同一基本DN中管理員角色成員的用戶來解決此問題。 希望可以幫到

相關問題