Spring LDAP - 在独立的java程序中创建LdapTemplate - 使用Spring LDAP作为CDI资源

时间:2014-03-16 10:55:24

标签: spring ldap spring-ldap

我正在尝试构造一个使用spring数据的LdapTemplate对象。

 public class LDAPTemplate {

        public static void main(String[] args) {
            LdapContextSource lcs = new LdapContextSource();
            lcs.setUrl("ldap://localhost:389/");
            lcs.setUserDn("cn=Manager, dc=example, dc=com");
            lcs.setPassword("secret1");
            lcs.setDirObjectFactory(DefaultDirObjectFactory.class);
            LdapTemplate ldap = new LdapTemplate(lcs);
            ldap.lookup("cn=aaa");

        }

    }

我想知道的是实例化ldap模板对象的正确方法。因为当我执行查找时,它会抛出NPE。

我试图在CDI上下文中使用LDAP Spring而根本不使用spring。如果你有指针就可以了。 Spring LDAP是否依赖于spring?

4 个答案:

答案 0 :(得分:4)

LdapContextSourceInitializingBean,因此您需要致电afterPropertiesSet ...

和JavaDoc:

  

在Spring Context之外使用此类的实现时   所有属性都需要调用afterPropertiesSet()   设置,以完成初始化。

答案 1 :(得分:4)

更正代码

public class LDAPTemplate {
    public static void main(String[] args) {
        LdapContextSource lcs = new LdapContextSource();
        lcs.setUrl("ldap://localhost:389/");
        lcs.setUserDn("cn=Manager, dc=example, dc=com");
        lcs.setPassword("secret1");
        lcs.setDirObjectFactory(DefaultDirObjectFactory.class);
        lcs.afterPropertiesSet();
        LdapTemplate ldap = new LdapTemplate(lcs);
        ldap.lookup("cn=aaa");

    }
}

答案 2 :(得分:0)

解决方案:在不使用Spring IoC的情况下在CDI竞赛中使用Spring LDAP

  1. 为LDAP模板创建资源生成器。

    public class Resources {
    private LdapTemplate template;
    
    @Produces
    //It is a custom qualifier
    @CustomeLDAPTemplate
    public LdapTemplate getTemplate() {
        LdapContextSource lcs = new LdapContextSource();
        lcs.setUrl("ldap://localhost:389/");
        lcs.setUserDn("cn=Manager, dc=example, dc=com");
        lcs.setPassword("secret1");
        lcs.setDirObjectFactory(DefaultDirObjectFactory.class);
        lcs.afterPropertiesSet();
        template = new LdapTemplate(lcs);
        return template;
    }
    
    public void setTemplate(LdapTemplate template) {
        this.template = template;
     }
    }
    
  2. 创建自定义限定符 - 要说我想要LdapTemplate和CustomeLDAPTemplate类型的tempate对象

    @Qualifier  
    @Retention(RUNTIME)  
    @Target({TYPE,CONSTRUCTOR, METHOD, FIELD})  
    public @interface CustomeLDAPTemplate {}  
    
  3. 实现 - 我使用JAX-WS类来验证。

    @Path("/users")
    @RequestScoped
    public class UserResource {
    
        @Inject
        @CustomeLDAPTemplate
        private LdapTemplate template;
    
        @POST
        @Consumes(MediaType.APPLICATION_XML)
        public Response createUser(InputStream is){
            User user = readStream(is);
            System.out.println("LDAP Look up " + template.lookup("cn=aaa,ou=Org1, dc=example, dc=com").toString());
            uRepo.save(user);
            return Response.created(URI.create("/users/" + user.getUser_id())).build();
        }   
    }
    

答案 3 :(得分:0)

 /**
 * contextSource
 * @return 
 */
@Bean
public LdapContextSource contextSource() {
    LdapContextSource contextSource = new LdapContextSource();
    contextSource.setUrl(properties.getProperty("ldap.url"));
    contextSource.setBase(properties.getProperty("ldap.base.dn"));
    contextSource.setUserDn(properties.getProperty("ldap.principal"));
    contextSource.setPassword(properties.getProperty("ldap.password"));
    contextSource.setReferral("ignore");
    return contextSource;
}

/**
 * Create Ldap Templelate Instance
 * @return 
 */
@Bean
public LdapTemplate ldapTemplate() {
    LdapTemplate ldapTemplate = new LdapTemplate();
    try {
        ldapTemplate = new LdapTemplate(contextSource());
    } catch (Exception e) {
        log.error("error while creating LDap Template", e);
    }
    return ldapTemplate;
}

/**
 * this Method check if the username and password are valid
 * then return either true if exists and false if not
 * @param username
 * @param password
 * @return
 */
public Boolean authenticateUser(final String username, final String password) {
    boolean auth = false;
    LdapTemplate ldapTemplate = new LdapTemplate(contextSource());
    try {
        ldapTemplate.setIgnorePartialResultException(true);
        log.info("ldapTemplate-->" + ldapTemplate);

        final AndFilter filter = new AndFilter().and(new EqualsFilter("objectclass", OBJECT_CLASS)).and(new EqualsFilter(NETWORK_USER_ENTITY, username));
        auth = ldapTemplate.authenticate(BASE_DN, filter.encode(), password);
        log.info("is Valid user :" + auth);
    } catch (Exception e) {
        log.error("error while creating LDap Template", e);
    }
    return auth;
}