900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > java spring ldap_Spring Boot连接LDAP服务器

java spring ldap_Spring Boot连接LDAP服务器

时间:2022-08-01 00:51:07

相关推荐

java spring ldap_Spring Boot连接LDAP服务器

LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP。它是基于X.500标准的,是一个开放、厂商中立的行业标准应用协议,其通过 IP 网络访问和维护分布式目录信息服务。与X.500不同,LDAP支持TCP/IP,这对访问Internet是必须的。LDAP的核心规范在RFC中都有定义,所有与LDAP相关的RFC都可以在LDAPman RFC网页中找到。

Spring Data LDAP提供了LDAP抽象。相关依赖包含在了spring-boot-starter-data-ldap Starter中。引入spring-boot-starter-data-ldap依赖,在pom.xml配置文件中增加如下内容(基于之前章节“Spring Boot 构建框架”中的pom.xml文件):

org.springframework.boot

spring-boot-starter-data-ldap

应用案例

连接LDAP服务器

连接到LDAP服务器,确保声明spring-boot-starter-data-ldap Starter或者spring-ldap-core引入相关依赖,然后在application.properties声明服务器的URL:spring.ldap.urls=ldap://myserver:1235

spring.ldap.username=admin

spring.ldap.password=secret

注意:如果需要自定义LDAP服务器连接设置,则可以使用spring.ldap.base和spring.ldap.base-environment属性。

Spring数据LDAP仓储

Spring Data包含了LDAP仓储支持。可以像任何其他Spring Bean一样注入一个自动配置的LdapTemplate实例,具体代码如下:@Component

publicclassMyBean{

privatefinalLdapTemplatetemplate;

@Autowired

publicMyBean(LdapTemplatetemplate){

this.template=template;

}

//...

}

嵌入式LDAP内存服务器

为了实现测试目的,Spring Boot支持从UnboundID自动配置LDAP内存服务器。要配置服务器,请添加com.unboundid:unboundid-ldapsdk依赖并声明一个base-dn属性,具体如下:spring.ldap.embedded.base-dn=dc=spring,dc=io

默认情况下,服务器将在一个随机端口上启动,并触发常规的 LDAP 支持(不需要指定spring.ldap.urls属性)。

如果classpath存在一个schema.ldif文件,其将用于初始化服务器。如果要从不同的资源加载中加载脚本,可以使用spring.ldap.embedded.ldif属性。

默认情况下,将使用一个标准模式(schema)来校验 LDIF 文件。使用spring.ldap.embedded.validation.enabled属性来完全关闭校验。如果有自定义的属性,可以使用spring.ldap.embedded.validation.schema来定义自定义属性类型或者对象类。

关于LDAP轻量目录服务概念及其原理分析参考资料:/yoodb/article/detail/1190

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。