java ad帐号禁用,如何通过LDAP请求启用或禁用AD用户帐户?

So far I was able to find users in LDAP but I don't know how can I enable or disable them.

As a second question, if my account has Domain Admin rights, I will be able to enable or disable account from LDAP or not?

Note: This is about a Microsoft Active Directory running on Windows 2003.

I know that I can check active uses with:

(!(useraccountcontrol:1.2.840.113556.1.4.803:=2))

Disabled useds:

(useraccountcontrol:1.2.840.113556.1.4.803:=2)

The question is how do I set the attribute in such way that it will not loose other binary flags inside.

解决方案

You need to use a bit of logic here. So to disable a user, you set the disable bit (2). So:

const long ADS_UF_ACCOUNTDISABLE = 0x00000002;

long userAccountControl = //currentUacValue

long newUserAccountControl = (userAccountControl | ADS_UF_ACCOUNTDISABLE);

To enable an account, we need to clear the disable bit:

long userAccountControl = //currentUacValue

long newUserAccountControl = (userAccountControl & ~ADS_UF_ACCOUNTDISABLE)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以通过使用LDAP(轻量目录访问协议)或者Active Directory(AD)的API来禁用用户。首先,需要确保已经建立了与LDAPAD服务器的连接。然后,可以使用以下步骤来禁用用户: 1. 通过提供的用户名在LDAPAD服务器上搜索用户。 2. 获取用户的属性,确认用户是否已被禁用。 3. 如果用户未被禁用,可以使用修改用户属性的方法来禁用用户。通常是将用户的状态设置为禁用状态或者将用户的账户锁定。 示例代码如下所示: ```java // 假设已建立了与LDAPAD服务器的连接 // 假设已经获取了用户名和密码 // 假设已经导入了必要的库和类 // 搜索用户 String username = "user123"; String searchFilter = "(&(objectClass=user)(sAMAccountName=" + username + "))"; SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration<SearchResult> results = ldapContext.search("DC=example,DC=com", searchFilter, searchControls); // 获取用户属性 if (results.hasMoreElements()) { SearchResult searchResult = (SearchResult) results.nextElement(); Attributes attributes = searchResult.getAttributes(); Attribute accountStatus = attributes.get("userAccountControl"); // 确认用户禁用 if (!isUserDisabled(accountStatus)) { // 禁用用户 modifyUserAccountControl(username); } } // 确认用户是否被禁用 private boolean isUserDisabled(Attribute accountStatus) { return ((Integer) accountStatus.get() & 2) > 0; } // 禁用用户 private void modifyUserAccountControl(String username) { ModificationItem[] mods = new ModificationItem[1]; mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userAccountControl", Integer.toString(514))); ldapContext.modifyAttributes("CN=" + username + ",OU=Users,DC=example,DC=com", mods); } ``` 以上示例代码演示了如何在Java中使用LDAPAD的API来禁用用户。首先通过用户名搜索用户,然后确认用户未被禁用,最后修改用户的属性来禁用用户

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值