Java实现FreeIpa(LDAP)增删改查

Java实现FreeIpa(LDAP)增删改查

一、首先需要初始化一个LdapContext对象

public static LdapContext context;
public static LdapContext getConn() {
   
        String username = Constant.LDAP_USERNAME;// Constant 从配置文件中获取数据
        String password = Constant.LDAP_PASSWORD;
        String server =  Constant.LDAP_SERVER;
        try {
   
            Hashtable<String, String> env = new Hashtable<String, String>();
            //admin用户名称
            env.put(Context.SECURITY_PRINCIPAL, username);
            //admin密码
            env.put(Context.SECURITY_CREDENTIALS, password);
            //url格式, ladp://ip:port
            env.put(Context.PROVIDER_URL, server);
            //LDAP工厂
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            //验证的类型     "none", "simple", "strong"
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            //启用ldap连接池
            env.put("com.sun.jndi.ldap.connect.pool", "true");
            //初始化ldap连接
            LdapContext ldapContext = new InitialLdapContext(env, null);
            return ldapContext;
        } catch (NamingException e) {
   
            e.printStackTrace();
            System.out.println("用户" + username + "登录验证失败");
            System.out.println("错误信息:"+e.getExplanation());
            return null;
        }
    }

二、添加操作

1. 添加用户

public static void addUser(HashMap<String,String> userMap) {
   
        context = getConn();//建立ldap连接
        try {
   
            String name = userMap.get("name");
            String email = userMap.get("email");
            String sn = name.substring(0, 1);
            String givenName 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值