我试图在枢纽配置LDAP作为JNDI资源



我通过ldap身份验证获得一些数据。我可以用这段代码正确地做到这一点。

List<Employees> emps=new ArrayList<Employees>();
    String url = "ldap://xxx:389";
    String base = "dc=xxx,dc=xxx,dc=xx";
    String userDn = "username";
    String password = "pass";
    try {
        LdapContextSource ctxSrc = new LdapContextSource();
        ctxSrc.setUrl(url);
        ctxSrc.setBase(base);
        ctxSrc.setUserDn(userDn);
        ctxSrc.setPassword(password);
        ctxSrc.afterPropertiesSet();
        LdapTemplate lt = new LdapTemplate(ctxSrc);
        AndFilter filter = new AndFilter();
        filter.and(new EqualsFilter("objectclass", "Person"));
        List<String> list = lt.search("", filter.encode(), new ContactAttributeMapperJSON());
        emps = new Gson().fromJson(list.toString(), new TypeToken<List<Employees>>() {
        }.getType());

这段代码可以正常工作。但是我想隐藏我的用户名和通行证。所以我从content.xml(枢纽)得到数据。这是我的content.xml:

<Resource name="ldap/LdapResource" auth="Container"
    type="javax.naming.ldap.LdapContext"
    factory="xxx"
    singleton="false" 
    java.naming.factory.initial="com.sun.jndi.ldap.LdapCtxFactory"
    java.naming.provider.url="ldap://xx:389"
    java.naming.security.authentication="simple"
    java.naming.security.principal="username"
    java.naming.security.credentials="pass." />

和新的代码块:

        List<Employees> emps = new ArrayList<Employees>();
    try {
        Context initialContext = new InitialContext();
        LdapContext ldapContext = (LdapContext) initialContext.lookup("java:comp/env/ldap/LdapResource");
        *LdapTemplate lt = new LdapTemplate(ldapContext);*
        AndFilter filter = new AndFilter();
        filter.and(new EqualsFilter("objectclass", "Person"));
        List<String> list = lt.search("", filter.encode(), new ContactAttributeMapperJSON());
        emps = new Gson().fromJson(list.toString(), new TypeToken<List<Employees>>() {
        }.getType());
这是我的问题。我不能LdapTemplate与LdapContext。它只工作LdapContextSource和我不能将LdapContext转换为LdapContextSource。我该怎么办?对不起,我的英语不好。谢谢你。

我在完全相同的情况下所做的是创建一个java.naming.spi.ObjectFactory子类,该子类使用Resource元素中给定的属性返回LdapContextSource实例。您可以使用来自'java.naming '的标准LDAP JNDI属性。或使用你自己的。

我唯一需要解决的是如何处理java.naming.security.authenticationjava.naming.security.protocol

最新更新