首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用IP地址属性的LDAP查询

使用IP地址属性的LDAP查询
EN

Stack Overflow用户
提问于 2014-03-02 17:13:18
回答 1查看 6.3K关注 0票数 1

我正在寻找一种使用用户IP地址查询LDAP的方法。

当有人使用浏览器时,浏览器会发送它的IP地址。我希望使用该IP地址查询LDAP,以查找该IP地址所属的用户名。

我已经设法使用Java中的LDAP连接到AD。

EN

回答 1

Stack Overflow用户

发布于 2014-03-03 09:01:50

请阅读EJP的评论,并重新考虑您的需求。

无论您为什么要这样做,您都需要采取以下几个步骤:

  • 查找用户所在的上下文(LDAP容器)。广告默认为cn=Users,dc=your,dc=domain,dc=com
  • 标识包含IP地址的LDAP属性(假设现在是networkAddress )
  • 从HTTP请求中检索IP地址(比方说String userAddress)
  • 使用过滤器(&(objectClass=inetOrgPerson)(networkAddress=userAddress))对(用户)对象执行查询

您的Java代码将如下所示(假设您有一个活动的LdapConnection对象,正如您提到的那样):

代码语言:javascript
复制
public void getUserByIp( LdapContext ctx, String userAddress )
{
  // Replace with your context and domain name
  String userContext = "cn=Users,dc=your,dc=domain,dc=com";

  String filter = "(&(objectClass=inetOrgPerson)(networkAddress="+userAddress+"))";
  // You are trying to find a single user, so set the controls to return only on instance
  SearchControls contr = new SearchControls();
  contr.setCountLimit( 1L );
  try
  {
    NamingEnumeration<SearchResult> results = ctx.search( userContext, filter, contr );
    while ( results.hasMore() )
    {
      // User found
      SearchResult user = results.next();
    } else {
      // No user found
    }
  } catch ( NamingException e ) {
      // If there is more than one result, this error will be thrown from the while loop
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22130678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档