使用证书和自定义 ssl 套接字工厂到 ldap 的 SSL 连接 [英] SSL connection to ldap using certitificates and custom ssl socket factory

查看:33
本文介绍了使用证书和自定义 ssl 套接字工厂到 ldap 的 SSL 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的场景,我想连接到 ldap usign jndi,我正在使用读取信任库和密钥库的自定义 SSLSOcketfactory.上下文创建成功,但是当我尝试使用相同的凭据进行身份验证时,它会抛出一个错误,指出不支持身份验证方法.

This is my scenario , i want to connect to ldap usign jndi , i am using custom SSLSOcketfactory which reads the truststore and keystore . The context is created successful but when i try to authenticate using the same credentials it throws an error telling that the authentication method is not supported.

这是我的自定义 ssl 套接字的代码 -

here is my code of the custom ssl socket -

try {
    StringBuffer trustStore = new StringBuffer("c:/Temp/certs/TrustStore");
            StringBuffer keyStore =  new StringBuffer("c:/Temp/certs/keystore.arun");
    StringBuffer keyStorePass = new StringBuffer("xxxxx");
               StringBuffer keyAlias = new StringBuffer("user");
        StringBuffer keyPass =  new StringBuffer("XXXX");

            TrustManagerFactory tmf =TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());

    FileInputStream fis = new FileInputStream(trustStore.toString());
    KeyStore ks1 = KeyStore.getInstance("jks");
    ks1.load(fis, trustStorePass.toString().toCharArray());
            fis.close();
    tmf.init(ks1);
    TrustManager[] tms = tmf.getTrustManagers();
    FileInputStream fin = new FileInputStream(keyStore.toString());
    KeyStore ks2 = KeyStore.getInstance("jks");
    ks2.load(fin, keyStorePass.toString().toCharArray());
    fin.close();
    KeyManagerFactory kmf =
        KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(ks2, keyStorePass.toString().toCharArray());
    KeyManager[] kms = kmf.getKeyManagers();
    if (keyAlias != null && keyAlias.length() > 0) {
            for (int i = 0; i < kms.length; i++) {
                // We can only deal with instances of X509KeyManager
                if (kms[i] instanceof X509KeyManager)
                    kms[i] = new CustomKeyManager(
                            (X509KeyManager) kms[i], keyAlias.toString());
            }
        }

SSLContext context = SSLContext.getInstance("TLS");
    context.init(kms,tms, null);
    ssf = context.getSocketFactory();
 } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

  public static SocketFactory getDefault() {

    return new CustomSSLSocketFactory();
}

使用这个CustomSSLSocketFactory的jndi代码如下

And the jndi code which uses this CustomSSLSocketFactory is as follows

    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldaps://wx64ads01a.vapps.esca.com:636");
    env.put(Context.REFERRAL, "follow");
env.put("java.naming.ldap.derefAliases", "always");
env.put("java.naming.ldap.factory.socket","com.eterra.security.authz.dao.CustomSSLSocketFactory" );

try {
    ctx = new InitialLdapContext(env, null);
// start ssl session for server authentication
    }catch(Exception e ){
    System.out.println(e);
}
    try{
ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION,
                    "EXTERNAL");
    String path = "CN=domain,DC=casa,DC=com"
    String inFilter = "(&(objectClass=*))";
     SearchControls sc = new SearchControls();
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = null;

results = ctx.search(path, inFilter, sc);
  }

我的上下文创建得很完美,但是当我尝试进行身份验证并绑定到 ldap 时,我得到了无效的身份验证方法.任何帮助将不胜感激,现在很长一段时间以来一直在努力解决这些错误.提前致谢.

My Context is created perfectly but when i try to authenticate and bind to the ldap , i get Invalid Authentication method . ANy help will be appreciated , Struggling with these error over a long time now . Thanks in advance .

推荐答案

Context.SECURITY_AUTHENTICATION,外部"

Context.SECURITY_AUTHENTICATION, "EXTERNAL"

当我尝试验证并绑定到 ldap 时,我得到了无效的验证方法

when i try to authenticate and bind to the ldap , i get Invalid Authentication method

所以您的 LDAP 服务器不支持外部身份验证.

So your LDAP server doesn't support EXTERNAL authentication.

这篇关于使用证书和自定义 ssl 套接字工厂到 ldap 的 SSL 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆