2017.6.8 spring-ldap基本使用总结
之前学习过spring-ldap的官方文档:2017.4.10 spring-ldap官方文档学习
现在是对实际使用的spring-ldap及使用过程中遇到的问题,进行总结。
1.spring-ldap的pom依赖
1 <!-- 添加Spring-ldap--> 2 <dependency> 3 <groupId>org.springframework.ldap</groupId> 4 <artifactId>spring-ldap-core</artifactId> 5 <version>2.3.1.RELEASE</version> 6 </dependency>
下面的网址是spring的ldap页,里面有一个quick start。其中显示了spring-ldap最近的版本,并且勾选不同的版本,会自动生成依赖。
http://projects.spring.io/spring-ldap/
2.ldapTemplate的生成--方式1自动注入
2.1 spring-ldap声明
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:ldap="http://www.springframework.org/schema/ldap" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 5 http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
2.2 ldapTemplate bean
1 <context:property-placeholder location="classpath:/ldap.properties" />
2 <ldap:context-source id="contextSource" 3 password="${sample.ldap.password}" 4 url="${sample.ldap.url}" 5 username="${sample.ldap.userDn}" 6 base="${sample.ldap.base}" />
7 <ldap:ldap-template id="ldapTemplate" context-source-ref="contextSource"/>
或者
1 <bean id="contextSource" 2 class="org.springframework.ldap.core.support.LdapContextSource"> 3 <property name="url" value="${sample.ldap.url}" /> 4 <property name="base" value="${sample.ldap.base}" /> 5 <property name="userDn" value="${sample.ldap.userDn}" /> 6 <property name="password" value="${sample.ldap.password}" /> 7 </bean> 8 9 <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate"> 10 <constructor-arg ref="contextSource" /> 11 </bean>
2.3 使用
1 @Autowired 2 private LdapTemplate ldapTemplate;
3.ldapTemplate的生成--方式2代码生成
1 public LdapTemplate getLdapTemplate(){ 2 LdapTemplate template = null; 3 try { 4 LdapContextSource contextSource = new LdapContextSource(); 5 contextSource.setUrl(getSync_ldap_url()); 6 contextSource.setBase(getSync_ldap_base()); 7 contextSource.setUserDn(getSync_ldap_userDn()); 8 contextSource.setPassword(getSync_ldap_password()); 9 contextSource.setPooled(false); 10 contextSource.afterPropertiesSet(); // important 11 template = new LdapTemplate(contextSource); 12 }catch (Exception e){ 13 e.printStackTrace(); 14 } 15 return template; 16 }
4.spring-ldap的使用
api的使用在上次的文档中已经学习过了:2017.4.10 spring-ldap官方文档学习
一些简单示例:
/** * 动态创建dn * @param user * @return */ private Name buildDn(User user) { return LdapNameBuilder.newInstance() .add("ou", "Users") .add("uid",user.getFdUserid().toString()) .build(); } /** * 在ldap里更新用户 * @param user */ private void updateUser(User user) { Name dn = buildDn(user); getLdapTemplate().rebind(dn, null, buildAttributes(user)); } /** * 在ldap里删除用户 * @param user */ private void deleteUser(User user) { Name dn = buildDn(user); getLdapTemplate().unbind(dn); } /** * 在ldap里创建用户 * @param user */ private void createUser(User user) { Name dn = buildDn(user); getLdapTemplate().bind(dn, null, buildAttributes(user)); } /** * 动态构建属性 * @param user * @return */ private Attributes buildAttributes(User user) { Attributes attrs = new BasicAttributes(); try { BasicAttribute objectclass = new BasicAttribute("objectclass"); objectclass.add("top"); objectclass.add("posixAccount"); objectclass.add("inetOrgPerson"); attrs.put(objectclass); attrs.put("userPassword", user.getFdLdapPassword() == null ? "" : user.getFdLdapPassword()); attrs.put("cn", user.getFdUsername() + "@" + user.getFdTenantName()); attrs.put("sn", user.getFdUsername() + "@" + user.getFdTenantName()); attrs.put("displayName", user.getFdDisplayName()== null? "":user.getFdDisplayName()); attrs.put("homeDirectory", "/root"); attrs.put("uidNumber", "0"); attrs.put("uid", user.getFdUserid().toString()); attrs.put("gidNumber", "0"); }catch (Exception e){ e.printStackTrace(); } return attrs; }
fighting for this