阅读背景:

Java验证Ldap账号和读取Ldap中所有用户

来源:互联网 

一、验证Ldap用户

public class LDAPAuthentication{    
    private final String URL = "ldap://192.168.1.205:389/";
    private final String BASEDN = "cn=demo1,dc=sys,dc=com";  // 根据自己情况进行修改
    private final String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
    private LdapContext ctx = null;
    private final Control[] connCtls = null;
  
    private void LDAP_connect() {
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
        env.put(Context.PROVIDER_URL, URL + BASEDN);
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        String root = "cn=demo1,dc=sys,dc=com";  // 根,根据自己情况修改
        env.put(Context.SECURITY_PRINCIPAL, root);   // 管理员
        env.put(Context.SECURITY_CREDENTIALS, "123456");  // 管理员密码
         
        try {
            ctx = new InitialLdapContext(env, connCtls);
            System.out.println( "认证成功" ); 
            System.out.println(ctx);
             
        } catch (javax.naming.AuthenticationException e) {
            System.out.println("认证失败:");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("认证出错:");
            e.printStackTrace();
        }
         
        if (ctx != null) {
            try {
                ctx.close();
            }
            catch (NamingException e) {
                e.printStackTrace();
            }
 
        }
    }
}
public class LDAPAuthenticatio



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: