2012-02-09 11 views
2

私の要件は、Webアプリケーションにアクセスできるようにユーザのログイン(ユーザ名&パスワード)を認証することです。admin/managerアカウントを使用しないSpring LDAP認証

boolean isCredentialsValid = false; 

Hashtable env = new Hashtable();   
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); 
env.put(Context.PROVIDER_URL, ldapUrl); 
env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
env.put(Context.SECURITY_PRINCIPAL, username); 
env.put(Context.SECURITY_CREDENTIALS, password); 

try { 
DirContext ctx = new InitialDirContext(env); 
ctx.close(); 
isCredentialsValid = true; 
} catch (Exception e) { 
e.printStackTrace(); 
} 

System.out.println(isCredentialsValid); 

が、これは直接春に実装することができる:

は、Javaを使用して、私は、ユーザーの提供ログインを使用して認証することができましたか? BindAuthenticatorやPasswordComparisonAuthenticatorのように?私はこの設定を使用しようとしました:

<bean id="contextSource" 
class="org.springframework.security.ldap.DefaultSpringSecurityContextSource"> 
    <constructor-arg value="IP of LDAP Server:389" /> 
</bean> 

<bean id="ldapAuthenticator" 
class="org.springframework.security.providers.ldap.authenticator.BindAuthenticator"> 
    <constructor-arg ref="contextSource"/> 
    <property name="userDNPatterns" > 
    <list> 
    <value>uid={0}, dc=company,dc=com</value> 
    </list> 
    </property> 
</bean> 

<bean id="userSearch" 
class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch"> 
    <constructor-arg index="0" value="ou=people" /> 
    <constructor-arg index="1" value="(uid={0})" /> 
    <constructor-arg index="2" ref="contextSource" /> 
    <property name="searchSubtree" value="true" /> 
</bean> 

しかし私はUncategorizedLdapExceptionをエンコーディングしています。

これまでのSpring LDAPで読んだことから、管理者アカウントを提供せずに直接ユーザーのログインを認証することはできません。

ご協力いただきありがとうございます。

これはスタックトレースです:

未分類の例外は、LDAP処理中に発生しました。ネストされた例外はjavax.naming.NamingExceptionです:[LDAP:エラーコード1 - 000004DC:LdapErr:DSID - 0C0906DC、コメント:この操作を実行するには、接続で正常なバインドを完了する必要があります、データ0、v1db0];残りの名前は 'uid = [email protected]'です。ネストされた例外はorg.springframework.ldap.UncategorizedLdapExceptionです:LDAP処理中に未分類の例外が発生しました。ネストされた例外はjavax.naming.NamingExceptionです:[LDAP:エラーコード1 - 000004DC:LdapErr:DSID - 0C0906DC、コメント:この操作を実行するには、接続で正常なバインドを完了する必要があります、データ0、v1db0];残りの名前は 'uid = [email protected]'

+0

Stacktraceしてください。 –

+0

@nico_ekitoスタックトレースを追加しました。ありがとう – Brenard

答えて

1

これはWindows用の場合はActive Directoryを試してくださいActiveDirectoryLdapAuthenticationProvider。

+0

ええ、それはWindows Active Directory用です。私はそれを確認し、私がそれを動作させることができるかどうか試します。どうもありがとう。 – Brenard

+1

@drobson将来的に無効になるかもしれないリンクだけでなく、あなたの答えに多くを含めてください。 – stuXnet

0

ldap(com.sun.security.auth.module.LdapLoginModule)用にJAASモジュールを試しましたか?

1

LdapTemplate(spring-ldap-core 1.3.1RELEASE)は、このシナリオでは役に立たない(悪いことに、他の人にとっても役に立たないと信じている) - LdapTemplate.authenticate検索は成功したバインディングがなければ完了できません。 LDAP

をキシにするためにSpringSecurityLdapTemplateを使用し、昔ながらのやり方

private static DirContext bindAsUser(String username, String password) { 
    final String bindUrl = url; 

    Hashtable<String, String> env = new Hashtable<String, String>(); 
    env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
    String bindPrincipal = username; 
    env.put(Context.SECURITY_PRINCIPAL, bindPrincipal); 
    env.put(Context.PROVIDER_URL, bindUrl); 
    env.put(Context.SECURITY_CREDENTIALS, password); 
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 

    try { 
    return contextFactory.createContext(env); 
    } catch (NamingException e) { 
    throw new RuntimeException(e); 
    } 
} 
  • とLDAPに対して

    1. の認証:

      私はActiveDirectoryLdapAuthenticationProviderのように同じ概念を適応しました

      もう1つの問題は、 ldap-core 1.3.1リリース、なぜ私はspring-security-ldapにマージされなかったのか分かりません:3.1。