《docker》docker安装LDAP

6 篇文章 0 订阅

1.OpenLDAP简介

LDAP是一款轻量级目录访问协议(Lightweight Directory Access Protocol,简称LDAP),属于开源集中账号管理架构的实现,且支持众多系统版本,被广大互联网公司所采用。

 

2.下载镜像 

# docker pull  osixia/openldap:1.2.2

3.启动容器

# docker run -p 389:389 -p 689:689 --name my-openldap \
 --env LDAP_ORGANISATION="my-company" \
--env LDAP_DOMAIN="my-company.com" \
--env LDAP_ADMIN_PASSWORD="123456" \
--detach osixia/openldap:1.2.2

4.测试

# docker ps |grep ldap

通过 LdapAdmin 连接

  dc=my-company,dc=com  //base

  cn=admin,dc=my-company,dc=com   // 用户名
  123456  // 密码

通过java连接测试

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

import org.springframework.ldap.NamingException;

public class LdapDemo {

	public static void main(String[] args) {
		try {
			connetLDAP();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (javax.naming.NamingException e) {
			e.printStackTrace();
		}
	}

	
	// 这个是连接我本机的ldap服务。实际使用中需要更改ldapUrl,ldapAccount,ldapPwd 三个参数
	public static LdapContext connetLDAP() throws NamingException, javax.naming.NamingException {
		// 连接Ldap需要的信息
		String ldapFactory = "com.sun.jndi.ldap.LdapCtxFactory";
		String ldapUrl = "ldap://192.168.3.205/dc=my-company,dc=com";// url
		String ldapAccount = "cn=admin,dc=my-company,dc=com"; // 用户名
		String ldapPwd = "123456";// 密码
		Hashtable<String, String> env = new Hashtable<String, String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, ldapFactory);
		// LDAP server
		env.put(Context.PROVIDER_URL, ldapUrl);
		env.put(Context.SECURITY_AUTHENTICATION, "simple");
		env.put(Context.SECURITY_PRINCIPAL, ldapAccount);
		env.put(Context.SECURITY_CREDENTIALS, ldapPwd);
		env.put("java.naming.referral", "follow");
		
		LdapContext ctxTDS = null;
		try {
			ctxTDS = new InitialLdapContext(env, null);
			System.out.println("认证成功");
		} catch (javax.naming.AuthenticationException e) {
			e.printStackTrace();
			System.out.println("认证失败");
		} catch (Exception e) {
			System.out.println("认证出错:");
			e.printStackTrace();
		}
		if (ctxTDS != null) {
			try {
				ctxTDS.close();
			} catch (NamingException e) {
				// ignore
			}
		}
		return ctxTDS;
	}

}

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值