openldap 2.4 只读账号

openldap 2.4 只读账号,第1张

由于OpenLDAP版本问题,本 *** 作适用于2.4版本,通过命令可以查看OpenLDAP版本号

1    slapd -VV  

1、OpenLDAP部署好以后默认只有一个管理员账号,而账号格式比较特殊,类似“cn=Manager,dc=hzins,dc=com”这样,这里的cn可以理解为用户名,dc类似于域名,整个结合起来像是Email格式一样。

2、创建系统用户和密码

1    useradd ldap_user  

2    echo "123456" | passwd –-stdin ldap_user  

3、提取用户信息,并生成ldif专用配置文件

1    grep “ldap_user” /etc/passwd >/root/users  

2    grep “ldap_user” /etc/group >/root/group  

3    /usr/share/migrationtools/migrate_passwd.pl /root/users >/root/users.ldif  

4    /usr/share/migrationtools/migrate_group.pl /root/groups >/root/groups.ldif  

4、修改配置文件第一行的dn,dc信息

1    vi /root/users.ldif  

2    dn: cn=ldap_user,dc=hzins,dc=com  #cn,dc都要修改为正确信息  

5、导入用户信息到数据库

1    ldapadd -x -w "123456" -D "cn=Manager,dc=myldap,dc=com" -f /root/users.ldif  #-w后面的密码是管理员密码,非新用户密码  

6、用phpldapadmin登录LDAP,这里用到的密码就是passwd命令所设置的密码了

增加用户

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

}


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

原文地址: http://outofmemory.cn/bake/11491412.html

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

发表评论

登录后才能评论

评论列表(0条)

保存