- Environment:
Oralce Identity Directory 11g
Spring 3.x --add the following ldap-related jar files in spring to your project
spring-ldap-core-tiger-1.3.0.RELEASE.jar
spring-ldap-1.3.0.RELEASE-all.jar
commons-lang-1.6.2.jar
commons-logging-1.0.4.jar
commons-logging-api-1.1.jar
org.springframework.beans-3.0.2.RELEASE.jar
org.springframework.core-3.0.2.RELEASE.jar
org.springframework.transaction-3.0.2.RELEASE.jar
- spring configuraiton
<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-2.0.xsd">
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="ldap://<server>:<port>" />
<property name="base" value="<base DN>" />
<property name="userDn" value="<user DN>" />
<property name="password" value="<password>" />
</bean>
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>
<bean id="ldapUserDao" class="com.ldap.dao.SsoUserDaoImpl">
<property name="ldapTemplate" ref="ldapTemplate" />
</bean>
</beans>
- Code
public void create(SsoUser user) {
try {
Attributes attrs = buildAttributes(user);
ldapTemplate.bind(buildDn(user), null, attrs);
} catch (Exception e) {
e.printStackTrace();
}
}
private Attributes buildAttributes(SsoUser user) {
Attributes attrs = new BasicAttributes();
BasicAttribute obj = new BasicAttribute("objectclass");
obj.add("top");
obj.add("person");
obj.add("cssoUser");
attrs.put(obj);
attrs.put("mail", user.getEmail());
attrs.put("employeenumber", user.getCustId());
attrs.put("cn", user.getUserId());
attrs.put("uid", user.getUserId());
attrs.put("sn", user.getUserId());
return attrs;
}
private String buildDn(SsoUser user) {
return "cn=" + user.getUserId();
}
// search user
@Override
public List<SsoUser> getAllUsers() {
return ldapTemplate.search("", "(objectclass=cssoUser)",
new SsoUserAttributeMapper());
}
private class SsoUserAttributeMapper implements AttributesMapper {
@Override
public Object mapFromAttributes(Attributes attrs)
throws NamingException {
SsoUser user = new SsoUser();
user.setUserId((String) attrs.get("cn").get());
user.setCustId((String) attrs.get("employeenumber").get());
user.setEmail((String) attrs.get("mail").get());
return user;
}
}
// update user
@Override
public void update(SsoUser user) {
try {
Attributes attrs = buildAttributes(user);
ldapTemplate.rebind(buildDn(user), null, attrs);
} catch (Exception e) {
e.printStackTrace();
}
}
No comments:
Post a Comment