之前学习过spring-ldap的官方文档:2017.4.10 spring-ldap官方文档学习

现在是对实际使用的spring-ldap及使用过程中遇到的问题,进行总结。

1.spring-ldap的pom依赖

         <!-- 添加Spring-ldap-->
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>

下面的网址是spring的ldap页,里面有一个quick start。其中显示了spring-ldap最近的版本,并且勾选不同的版本,会自动生成依赖。

http://projects.spring.io/spring-ldap/

2.ldapTemplate的生成--方式1自动注入

2.1 spring-ldap声明

 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">

2.2 ldapTemplate bean

 <context:property-placeholder location="classpath:/ldap.properties" />
<ldap:context-source id="contextSource"
password="${sample.ldap.password}"
url="${sample.ldap.url}"
username="${sample.ldap.userDn}"
base="${sample.ldap.base}" />
<ldap:ldap-template id="ldapTemplate" context-source-ref="contextSource"/>

或者

 <bean id="contextSource"
class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="${sample.ldap.url}" />
<property name="base" value="${sample.ldap.base}" />
<property name="userDn" value="${sample.ldap.userDn}" />
<property name="password" value="${sample.ldap.password}" />
</bean> <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>

2.3 使用

 @Autowired
private LdapTemplate ldapTemplate;

3.ldapTemplate的生成--方式2代码生成

 public LdapTemplate getLdapTemplate(){
LdapTemplate template = null;
try {
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl(getSync_ldap_url());
contextSource.setBase(getSync_ldap_base());
contextSource.setUserDn(getSync_ldap_userDn());
contextSource.setPassword(getSync_ldap_password());
contextSource.setPooled(false);
contextSource.afterPropertiesSet(); // important
template = new LdapTemplate(contextSource);
}catch (Exception e){
e.printStackTrace();
}
return template;
}

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;
}

2017.6.8 spring-ldap基本使用总结的更多相关文章

  1. Spring LDAP

    LDAP Spring LDAP 使用 - Sayi像秋天一样优雅 - 开源中国社区 http://docs.spring.io/spring-ldap/docs/current/reference/ ...

  2. Spring LDAP的使用

    LDAP入门http://www.jianshu.com/p/7e4d99f6baaf Spring LDAP,是Spring的一个组件,实现对LDAP的操作. 在编程操作MySQL时,我们除了用JD ...

  3. 2017技术核心——Spring

    从毕业从事Java WEB开始到现在已差不多快5年时间了,一直使用的Spring相关的技术,其实最主要的是SpringMVC这一块.其实,一直停留在用的地步,并不知晓其原理,真正耐下心来去研究Spri ...

  4. Spring Ldap 的增删改查

    package ldap.entity; /** * 本测试类person对象来自schema文件的core.schema文件 * objectClass为person,必填属性和可选属性也是根据该对 ...

  5. 2017.3.31 spring mvc教程(八) <mvc:annotation-driven />所做的工作

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  6. 2017.3.31 spring mvc教程(七)多视图控制器

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  7. 2017.3.31 spring mvc教程(六)转发、重定向、ajax请求

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  8. 2017.3.31 spring mvc教程(五)Action的单元测试

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  9. 2017.3.31 spring mvc教程(四)全局的异常处理

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  10. 2017.3.31 spring mvc教程(三)拦截器

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

随机推荐

  1. html li css选中状态切换

    思路:点击当前li元素后是用removeClass()删除所有兄弟元素(使用siblings()获取)的class样式,然后使用addClass()为当前li添加class. 具体演示如下: 1.HT ...

  2. set(NOIP模拟赛Round 4)

    这题很神奇,对吧. 标程还理解了好久,才明白. 这道题需要用状压DP.首先我们看到总共只有15个字符串,所以可以用hash存储状态. 然后我们还需要一维用来存储DP到第几个字符. 所以dp[i][j] ...

  3. linux进程地址空间--vma的基本操作【转】

    转自:http://blog.csdn.net/vanbreaker/article/details/7855007 版权声明:本文为博主原创文章,未经博主允许不得转载. 在32位的系统上,线性地址空 ...

  4. (十一)Ubuntu下面怎么找到一个软件安装的目录,卸载软件

    aptitude show packagename 实例: aptitude show sublime-text-installer 可以看到这个软件一系列信息 dpkg命令 dpkg -l //列车 ...

  5. (十二)C语言双指针的常见用法

    1.用作函数的返回值,比较常见的是返回分配的堆内存地址. 下面用一个例子进行说明下: /******************************************************** ...

  6. springMVC 配置中易犯的小错误

    搭建springMVC环境时有可能遇到:'警告: No mapping found for HTTP request with URI [/WEB-INF/pages/helloWorld.jsp] ...

  7. 关于js拖拽功能,拖拽元素的position:fixed;left:0;right:0;样式引起左右拖动元素会出现落后鼠标移动距离的问题

    被拖拽元素的样式如果为:position:fixed;left:0;right:0;(当时是为了让fixed定位的元素水平居中加的left:0;right:0;避免js动态计算定位的麻烦)时左右拖动会 ...

  8. RMQ-ST算法

    1068 : RMQ-ST算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在美国旅行了相当长的一段时间之后,终于准备要回国啦!而在回国之前,他们准备去 ...

  9. HDU5469 Antonidas(树分治&&哈希)

    给你一颗点上有字符的树,问一个给定的字符串是否是这棵树上的两点的路径. 树分治的思想就是每次找重心,重心下的子问题分解去做,然后就是合并了.合并的时候用一个总的set<pair<len,h ...

  10. Appium+python自动化10-AVD 模拟器【转载】

    前言 有些小伙伴没android手机,这时候可以在电脑上开个模拟器玩玩 一.模拟器配置 1.双击启动AVD Manager,进入配置界面