我正在寻找一种使用用户IP地址查询LDAP的方法。
当有人使用浏览器时,浏览器会发送它的IP地址。我希望使用该IP地址查询LDAP,以查找该IP地址所属的用户名。
我已经设法使用Java中的LDAP连接到AD。
发布于 2014-03-03 09:01:50
请阅读EJP的评论,并重新考虑您的需求。
无论您为什么要这样做,您都需要采取以下几个步骤:
cn=Users,dc=your,dc=domain,dc=com。networkAddress )String userAddress)(&(objectClass=inetOrgPerson)(networkAddress=userAddress))对(用户)对象执行查询您的Java代码将如下所示(假设您有一个活动的LdapConnection对象,正如您提到的那样):
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
}
}https://stackoverflow.com/questions/22130678
复制相似问题