java 实现对ad域的操作

(若要修改密码、修改权限等其他操作,需要先导出ad域的证书,并导入到jdk中,连接时使用636端口号(默认),使用ldaps协议进行连接)

1. 初始化AD域服务连接

    public static LdapContext getLdapContext() {
        Properties properties= new Properties();
        String ldapUserName = "";//AD管理员系统的账号
        String ldapPassword = "";//AD管理员系统的password
        String ldapIP = "127.0.0.1";//ad域的ip地址
        String ldapPost = "389";//ad域的port ,默认为389
        String ladpSecurityAuthentication = "simple";

        String ldapURL = "ldap://" + ldapIP + ":" + ldapPost;// ldap://ip:port
        
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");// LDAP工厂类
        properties.put(Context.SECURITY_AUTHENTICATION, ladpSecurityAuthentication);//LDAP访问安全级别:"none","simple","strong"
        properties.put(Context.SECURITY_PRINCIPAL, ldapUserName);
        properties.put(Context.SECURITY_CREDENTIALS, ldapPassword);
        properties.put(Context.PROVIDER_URL, ldapURL);
        try {
            return new InitialLdapContext(properties, null);
        } catch (Exception e) {
            LOG.info("AD域服务连接认证失败");
            e.printStackTrace();
        }
        return null;
    }
    

如果执行以上代码没有抛出异常,则连接成功

2. 关闭ad域连接

    /**
     * @Description:关闭AD域服务连接
     * @author qw
     * @date 2022-03-25
     */
    public static void close(LdapContext lc) {
        if (lc != null) {
            try {
                lc.close();
            } catch (NamingException e) {
                LOG.info("" + e);
            }
        }
    }

3. 添加ad域用户

  • 根据完整的可分辨名称进行添加
    例如(CN=test001,OU=测试部门01,OU=测试部门,DC=qw,DC=com)
    在这里插入图片描述

名称对应关系如下
在这里插入图片描述

    public static void add() {
        LdapContext lc = getLdapContext();
        try {
            Attributes attrs = new BasicAttributes(true); //参数集合
            attrs.put("objectClass", "user");
            attrs.put("samAccountName", "test001");
            attrs.put("displayName", "test001");
            lc.createSubcontext("CN=test001,OU=测试部门01,OU=测试部门,DC=qw,DC=com", attributes);
        } catch (Exception e) {
            e.printStackTrace();
            LOG.info("新增AD域用户失败:" + e);
        } finally {
            close(lc);
        }
    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-Juan-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值