从Spring连接LDAP [英] Connect LDAP from Spring

查看:288
本文介绍了从Spring连接LDAP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须实现一个基于Spring的Web应用程序,允许用户管理LDAP数据.到LDAP的连接只能使用JNDI框架(不允许使用SpringLDAP)进行.

I have to realize a web application based on Spring, allowing the user to manage LDAP data. The connection to the LDAP should be done only with the JNDI framework (no SpringLDAP allowed).

为此,我实现了一个实用程序类来执行基本操作(添加,更新,删除,列表等).

For this, I realized a utility class to do the basic operations (add, update, delete, list, ...).

以下是此类的一小段代码:

Here is a short block of code of this class :

public class LdapUtility {


    private static LdapUtility instance;

    private DirContext dirContext;


    public static LdapUtility getInstance() {

        if(LdapUtility.instance == null)
            LdapUtility.instance = new LdapUtility();

        return LdapUtility.instance;
    }

    /**
     * Connect to the LDAP
     */
    private LdapUtility() {

        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://localhost:389");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,dc=my-domain,dc=com");
        env.put(Context.SECURITY_CREDENTIALS, "secret");

        try {
            dirContext = new InitialDirContext(env);
        }
        catch(Exception ex) {
            dirContext = null;
        }
    }

    public void addUser(User u) {

           dirContext.createSubcontext(....); //add user in the LDAP
    }
}

使用此代码,我可以通过调用LdapUtility.getInstance()...访问我的所有方法,但是与LDAP的连接将永远不会释放.

With this code, I can access all my methods by calling LdapUtility.getInstance()..., but the connection to the LDAP will never be released.

另一种方法是在每次操作之前连接到LDAP,但是在这种情况下,与LDAP的连接太多...

Another way would be to connect to the LDAP before each operation, but in this case there would be too much connections to the LDAP...

所以,这是我的问题:访问这些方法的最优雅/最聪明的方法是什么?

So, here is my question : what is the most elegant/smartest way to access these methods ?

预先感谢您:-)

推荐答案

没有弹簧(被禁止),我会很快实现一些类似的东西:

without a spring (being forbidden), i would quickly implement something simillar:

  • (在懒惰时)创建一个简单的回调接口(例如,您可以在春季找到-JpaCallback.execute(EntityManager em))-但对于LDAP-MyLdapCallback.execute(LdapConnection连接)-LdapConnection的接口您可以想像您需要的任何东西-来自OpenLdap或SDK上下文的对象.诸如此类(仅用于演示):
...
interface LdapCallback<T> {
    T execute(DirContext ctx) throws NamingException, IOException;
}
...
private <T> T execute(LdapCallback<T> callback) throws NamingException, IOException {
    T result = null;
    LdapContext ctx = new InitialLdapContext();
    try {
        result = callback.execute(ctx);
    } finally {
        if (tls != null) {
            tls.close();
        }
        ctx.close();
    }
    return result;
}
...

完成后,您将为每个Ldap调用创建一个匿名类,并通过execute(callback)调用回调.

Once done, you will create anonymous classes for each Ldap call an call the callback via execute(callback).

  • (有更多时间)实施广告1.+创建AOP,将标有注解的方法包装成具有方面的元素,该方法本身将在上述包装器中执行我的方法(在我的代码中未明确这样做)

这篇关于从Spring连接LDAP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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