首页 文章

如何停用LDAP用户?

提问于
浏览
2

我正在使用库来验证LDAP用户,其代码如下:

public void authUser(String username, String pwd)
    throws Exception
  {
    try
    {
      Properties env = getEnvironmentForContext();

      env.put("java.naming.security.principal", "uid=" + 
      username + ",ou=users, dc=company"));
      env.put("java.naming.security.credentials", pwd);
      context = getContext(env);
      System.out.println("Authentication Succeeded");
    }
    catch (Exception e)
    {
      System.out.println("Authentication Failed");
      throw e;
    }
  }

请注意,我无法修改上述验证码 . 它来自外部图书馆 .

但是,我想停用一些用户(而不是删除它们),以便身份验证失败 . 我正在使用LDAP(不是Active Directory) . 不知道它是什么LDAP软件,我可以使用“LDAP浏览器客户端”连接到它 .

用户存在于:dc = company,ou = users,uid = username

What attribute can i add/change on LDAP 'user' to de-activate a user.
我可以将用户移动到另一个组,例如:dc = company,ou = deactivatedusers,uid = username?但这不是首选方案,加上我不确定最佳方式 .

编辑:正在使用的LDAP是:Netscape / Sun / iPlanet

4 回答

  • 0

    要根据Oracle iPlanet(Sun)文档回答您的问题:

    将nsAccountLock属性设置为true将禁用用户帐户,并阻止它们绑定到目录 .

    但是,就你已经拥有的代码而言,我只是没有看到任何实现这一点的方法......是否有什么东西阻止你使用.Net中的 System.DirectoryServices.Protocols 命名空间编写自己的iPlanet实现?

    以下是我如何使用iPlanet服务器绑定和授权用户:

    //Build servername from variables
    var BuildServerName = new StringBuilder();
    BuildServerName.Append(ServerName);
    BuildServerName.Append(":" + Convert.ToString(Port));
    
    var ldapConnection = new LdapConnection(BuildServerName.ToString());
    //Authenticate the Admin username and password, making sure it's a valid login
    
    try
    {
        //Pass in the network (administrative) creds, and the domain.
        var networkCredential = new NetworkCredential(Username, Password, config.LdapAuth.LdapDomain);
        ldapConnection.SessionOptions.SecureSocketLayer = true;
        ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };
        ldapConnection.AuthType = AuthType.Anonymous;;
        ldapConnection.Bind(networkCredential);
    
        //Lets find this person so we can use the correct DN syntax when we authorize them.
        SearchRequest FindThem = new SearchRequest();
        FindThem.Filter = config.LdapAuth.LdapFilter.Replace("{{Patron}}", Patron);
        FindThem.DistinguishedName = config.LdapAuth.LdapDomain;
        FindThem.Scope = System.DirectoryServices.Protocols.SearchScope.Subtree;
    
        //We'll execute a search using the bound user
        SearchResponse searchresults = (SearchResponse) ldapConnection.SendRequest(FindThem);
    
        //Should only get on result back, if not throw an error
        if(searchresults.Entries.Count == 1)
        {
             SearchResultEntryCollection entries = searchresults.Entries;
             SearchResultEntry thispatron = entries[0];
             PatronDN = thispatron.DistinguishedName;
        }
     }
    

    如果要将禁用的用户移动到特定组,从这一点开始,您可以编写逻辑来检查该用户的 DistinguishedName ,如果他们的 DistinguishedName 包含该组的名称,则抛出已处理的异常 . 此外,如果 nsAccountLock 属性作为可读属性可用于绑定帐户,则只需检查 true 的该属性的值,并相应地处理用户 .

  • -1

    如果目录软件支持密码策略功能,则它可能提供用于锁定/停用用户的属性 . 如果没有,您可以简单地取消密码属性(例如,userpassword) . 执行经过身份验证的绑定时,LDAP服务器应向客户端返回“不正确的身份验证”错误 .

  • 0

    以下是使用JNDI在Active Directory中禁用和启用用户的Java代码 . 在调用以下代码之前,请务必与AD连接 .

    public void disableEnableUser() throws Exception {
    ModificationItem[] mods = new ModificationItem[1];
                //To enable user
                //int UF_ACCOUNT_ENABLE = 0x0001;
                //mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userAccountControl",Integer.toString(UF_ACCOUNT_ENABLE)));
    
            // To disable user
            int UF_ACCOUNT_DISABLE = 0x0002;
            mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userAccountControl",Integer.toString(UF_ACCOUNT_DISABLE)));
    
            ctx.modifyAttributes("CN=John ABC,OU=Users,OU=anyone,DC=yourcompanyname,DC=com", mods);
        }
    

    专有名称=“CN = John ABC,OU = Users,OU = any,DC = yourcompanyname,DC = com”此名称取决于您的Active Directory结构,您可以从您的支持团队确认 .

  • 1

    只需更改用户密码即可 .

相关问题