2015-04-07 152 views
3

我正在构建一个应用程序,我需要使用UnboundID连接到Active Directory。使用example,我设法连接用户与他们的distinguishedNamepassword使用用户名使用UnboundID验证Active Directory用户

但是,我想仅使用domainusername进行身份验证,类似于Windows中的操作。使用名为JXplorer的工具浏览AD,好像sAMAccountName可能是我需要的属性。但用sAMAccountName替换distinguishedName导致出现AcceptSecurityContext错误。使用示例中显示的"uid=..."语法也产生了相同的错误。

有没有办法仅使用域登录,username/sAMAccountNamepassword。或者我需要通过AD进行搜索并找到我想验证的用户的distinguishedName,然后使用他们的distinguishedNamepassword绑定连接?

+1

不同于传统的LDAP这就需要DN的简单绑定:只要用它代替DN对绑定sAMAccountName直接在一个简单的绑定中(所以不需要,你不需要搜索DN)。但由于各种原因,使用LDAP进行身份验证并不理想。像[Jespa](http://www.ioplex.com/)这样的东西将是正确的方式,因为它模仿了Windows如何认证客户端。 – squarewav

+0

这是一个有趣的选择,如果我们计划使用AD来管理用户权限,我可能会考虑它。此刻,这也没有使用IE SSO是一种选择,所以它似乎有点矫枉过正。 我想知道你是如何设法在简单的绑定中使用sAMAccountName的。毕竟是我原来的问题的目的。 – Anders

回答

6

如@ioplex在他的评论说,AD接受使用从sAMAccountName赋的用户名与所附域名的绑定到它。 ,AD许可使用

String userId = username + "@" + domain; 
SimpleBindRequest adminBindRequest = new SimpleBindRequest(userId, passsword); 

最终用户id将会像“[email protected]

+0

这不适合我。据我所知,使用@符号Microsofts AD服务器尝试匹配电子邮件地址。这没有错,因为电子邮件地址是AD中的唯一标识符,但当您的电子邮件域与AD域不同时,它看起来很尴尬。最后我使用了这种方法'String userId = username +“\\”+ domain'。 偏题:我最初的麻烦原因是我们的域名和服务器的DNS地址不同。没有人记得告诉我:( – Anders

5

您将需要使用具有适当权限的帐户执行搜索samAccountName来查找用户,然后使用专有名称绑定为找到的用户。

您需要确保只从搜索中返回一个条目。

样本仅供演示!

参数会是这样的:

“adldap.example.com”, “CN =鲍勃,OU =用户,DC =例如,DC = COM”, “connPwd”,“OU =用户,DC =例如,DC = COM” “的samAccountName” “findUserValue” “的userPassword”

/** 
* @author jwilleke <br/> 
*   Use For Demonstration Purposes ONLY! 
* @param args 
*/ 
public static void main(String[] args) 
{ 
String connHost = args[0]; 
String connID = args[1]; 
String connPwd = args[2]; 
String searchBase = args[3]; 
String findUserByAttribute = args[4]; 
String findUserValue = args[5]; 
String userPassword = args[6]; 
int connPort = 389; 

// TODO Auto-generated method stub 
String actualLDAPServer = null; 
RootDSE rootDSE = null; 
// If I were doing this for real, I would use a POOL for Connections 

SSLUtil sslUtil = new SSLUtil(new TrustAllTrustManager()); // Use For Demonstration Purposes ONLY! 
SSLSocketFactory sslSocketFactory = null; 
try 
{ 
    sslSocketFactory = sslUtil.createSSLSocketFactory(); 
} 
catch (GeneralSecurityException e1) 
{ 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} 
SimpleBindRequest adminBindRequest = new SimpleBindRequest(connID, connPwd); 
LDAPConnection adminConnection = new LDAPConnection(sslSocketFactory); 
try 
{ 
    adminConnection = new LDAPConnection(connHost, connPort); 
    log.debug("Successful LDAP adminConnection to:" + connHost + ":" + connPort); 
    adminConnection.bind(adminBindRequest); 
    log.debug("Successful Bind as:" + connID); 
} 
catch (LDAPException e) 
{ 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

LDAPConnection userConnection = new LDAPConnection(sslSocketFactory); 
try 
{ 
    userConnection = new LDAPConnection(connHost, connPort); 
    log.debug("Successful LDAP userConnection to:" + connHost + ":" + connPort); 
} 
catch (LDAPException e) 
{ 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
// Construct Filter to find user 
Filter findUserfilter = null; 
findUserfilter = Filter.createEqualityFilter(findUserByAttribute, findUserValue); 
// Create Search Request 
SearchRequest searchRequest = new SearchRequest(searchBase, SearchScope.SUB, findUserfilter); 
searchRequest.setSizeLimit(1); // We will error if we get more than one hit 
SearchResult searchResult = null; 
try 
{ 
    searchResult = adminConnection.search(searchRequest); 
} 
catch (LDAPSearchException e) 
{ 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
String userDN = null; 
if (searchResult.getEntryCount() > 1) 
{ 
    log.error("We got more than one Entry for:" + searchRequest.getFilter()); 
} 
if (searchResult.getEntryCount() == 0) 
{ 
    log.error("We got No Entries for:" + searchRequest.getFilter()); 
} 
for (SearchResultEntry entry : searchResult.getSearchEntries()) 
{ 
    userDN = entry.getDN(); 
    log.debug("Found an Entry: " + userDN); 
} 
SimpleBindRequest userBindRequest = new SimpleBindRequest(userDN, userPassword); 
if (userBindRequest.getBindDN() == null) 
{ 
    log.warn("We got a null for the userBindRequest UserDN and therefore the bind is anonymous !"); 
} 
if (userBindRequest.getPassword() == null) 
{ 
    log.warn("We got a null for the userBindRequest Password and therefore the bind is anonymous !"); 
} 
try 
{ 
    userConnection.bind(userDN, userPassword); 
    log.debug("Successful userConnection Bind as:" + userDN); 
} 
catch (LDAPException e) 
{ 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
} 

-Jim

+0

你有这样的代码示例。我尝试使用此适应:http://stackoverflow.com/questions/11665418/how-to-get-dn-in-ldap-with-user-id-using-unboundid-ldap-sdk,但我的搜索保持返回一个空对象。 – Anders

相关问题