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
}
while(time>=0){hour=time/3600
minute=(time-hour*3600)/60
seconds=time-hour*3600-minute*60
jl1.setText(hour+"时")
jl2.setText(minute+"分")
jl3.setText(seconds+"秒")
try {
Thread.sleep(1000)
} catch (InterruptedException e) {
e.printStackTrace()
}
time--
}
这个问题很可能在于你搭建的LDAP域没有设置完整,如果没有配置好dns,那么netbios或wins之类的要配置正常.
建议配置必要的dns选项.
另外,不知道你是据据哪些工具或资料搭建的.
建议用samba3,或4
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)