LDAP如何增加用户

LDAP如何增加用户,第1张

增加用户

public boolean addUserToLdap(String userCn) {

LdapContext ctx = getLdapConnection() //AD认证,获取Ldap连接对象ctx

Attributes attrsbu = new BasicAttributes(true)

BasicAttribute objclassSet = new BasicAttribute("objectclass")

for (String _class : LdapConfig.userObjectClass.split(",")) {

objclassSet.add(_class.trim()) //加入一些基本元素top,person,organizationalPerson,user

}

boolean isExist = this.searchUser(ctx, userCn)//添加用户前先查找AD中是否存在该用户

if (isExist == false){

attrsbu.put(objclassSet)

attrsbu.put(new BasicAttribute("cn", userCn)) //userCn:要添加的用户

attrsbu.put(new BasicAttribute("sAMAccountName", userCn))

attrsbu.put(new BasicAttribute("sn", userCn))

attrsbu.put(new BasicAttribute("displayName", userCn))//AD中显示的名称

attrsbu.put(new BasicAttribute("userPrincipalName", userCn + LdapConfig.domainName))

try {

String userDn = "cn=" +userCn + ","+ LdapConfig.userRootDn

ctx.createSubcontext(userDn, attrsbu) //添加用户到Ldap

System.out.println("[addUserToLdap] Add User:" + userDn)

ModificationItem[] mods = new ModificationItem[2]

//此密码必须注意:得到的字符串是""password""这种格式

String newQuotedPassword = LdapConfig.user_default_password

byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE")

mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,

new BasicAttribute(LdapConfig.password_field,newUnicodePassword))

mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,

new BasicAttribute(LdapConfig.userAccountControl,Integer.toString(

UF_NORMAL_ACCOUNT

+ UF_PASSWORD_EXPIRED

+ UF_DONT_EXPIRE_PASSWD)))

ctx.modifyAttributes(userDn, mods) //更改用户密码和权限

} catch (Exception e1){

e1.printStackTrace()

destroyLdapConnection(ctx)

return false

}

}else{

System.out.println("isExist :" + isExist + "用户:" + userCn + "已存在")

}

destroyLdapConnection(ctx)

return true

}

需要。

使用非加密的ldap时,只能对AD域账号信息查询。如果要对AD域用户信息进行修改和新增 *** 作,必须使用(SSL)加密方式连接AD,需满足如下几个条件:① AD上需要安装证书服务。② 连接AD的主机上使用http://myhost.com/certsrv/打开浏览器申请证书。③ 如果连接AD的主机是Linux,需要安装openssl包,制作CA证书

微软官方给出的ldap修改密码方案:密码存储在 Active Directory 用户中的对象的 unicodePwd属性。可以在有限的情况下,写入此属性,但无法读取。该特性只能修改 ;不能将添加的对象的创建日期或通过搜索查询。 若要修改此属性 ,则客户端必须 128 位安全套接字层 (SSL) 连接到服务器 。为此连接成为可能,服务器必须拥有一个 128 位的 RSA 连接的服务器证书,客户端必须信任生成服务器证书的证书颁发机构 (CA) 和客户端和服务器必须能够使用 128 位加密。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11644137.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存