Hi, I'm new to Spring LDAP, and having a bit of configuration problems;
First I tried to setup SpringLDAP using Autowire
applicationContect
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="countrycerts"/>
<mvc:annotation-driven />
<context:property-placeholder location="classpath:config.properties" />
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="messages"/>
<import resource="persistence.xml"/>
<import resource="ldap.xml"/>
</beans>
ldap.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<property name="contextSource" ref="contextSource" />
</bean>
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="ldap://localhost:10389" />
<property name="userDn" value="uid=admin,ou=system" />
<property name="password" value="d3v3l0p3r" />
<property name="pooled" value="false" />
</bean>
<bean id="ldapBean" class="countrycerts.beans.LDAPBean">
<property name="ldapTemplate" ref="ldapTemplate"/>
</bean>
</beans>
But the DI failed, or what i'm currently thinking that its value was reset. I tried the "old way" of hardwiring the dependencies, and i can see the dependency being set,
Code:
set LDAP template: org.springframework.ldap.core.LdapTemplate@5010f992
but after initialisation the call fails on a null ldaptemplate
LDAPBean.java
Code:
package countrycerts.beans;
import java.util.HashSet;
import java.util.Set;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.stereotype.Service;
import countrycerts.ldap.User;
import countrycerts.ldap.UserAttributesMapper;
@ManagedBean
@SessionScoped
@Service
public class LDAPBean {
//@Autowired
private LdapTemplate ldapTemplate;
public void setLdapTemplate(LdapTemplate template) {
System.out.println("set LDAP template: " + template);
this.ldapTemplate = template;
}
public void getUsers() {
System.out.println("ldapTemplate:" + this.ldapTemplate);
add("New User", "User", "12345");
Set<User> users = getAllUsers("David");
for (User user: users){
System.out.println(user.getCommonName() + "-" + user.getTelephone());
}
}
@SuppressWarnings("unchecked")
public Set<User> getAllUsers(String surName){
UserAttributesMapper mapper = new UserAttributesMapper();
AndFilter filterObject = new AndFilter();
filterObject.and(new EqualsFilter("objectClass", "person"));
filterObject.and(new EqualsFilter("sn", surName));
return new HashSet<User>(
this.ldapTemplate.search("ou=users,ou=system", filterObject.encode(), mapper));
}
public void add(String commonName, String surName, String telephone){
String baseDn = "ou=users,ou=system";
DistinguishedName distinguisedName = new DistinguishedName(baseDn);
distinguisedName.add("cn", commonName);
Attributes userAttributes = new BasicAttributes();
userAttributes.put("sn", surName);
userAttributes.put("telephoneNumber", telephone);
BasicAttribute classAttribute = new BasicAttribute("objectclass");
classAttribute.add("top");
classAttribute.add("person");
userAttributes.put(classAttribute);
this.ldapTemplate.bind(distinguisedName, null, userAttributes);
}
}
Am I missing something in my configuration or class?