java 测试 LDAP 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14146833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing ldap connection
提问by user1366786
I want to validate user entered ldap settings. On settings page user enters ldap url, manager dn and password. I have a 'Test settings' button on this page so that user can quickly verify the ldap connection. How to do this easily and quickly?
我想验证用户输入的 ldap 设置。在设置页面上,用户输入 ldap url、manager dn 和密码。我在此页面上有一个“测试设置”按钮,以便用户可以快速验证 ldap 连接。如何轻松快速地做到这一点?
Our application using spring security and in the process of adding ldap authentication to it. I am kind of new to java and ldap, so pointing me to right direction is greatly appreciated.
我们的应用程序使用 spring security 并正在向它添加 ldap 身份验证。我对 java 和 ldap 有点陌生,因此非常感谢为我指出正确的方向。
Thanks.
谢谢。
回答by Marcel St?r
Based on the information given it is hard to tell what you know and what you don't know yet. So, I suggest you follow this helpful tutorial at java.net LdapTemplate: LDAP Programming in Java Made Simpleand skip the chapters not relevant to you (it's from 2006 but still ok). Spring LDAPreferenced in the article is at version 1.3.1 by now.
根据所提供的信息,很难说出您知道什么和您不知道什么。因此,我建议您遵循 java.net LdapTemplate 上的这个有用教程:Java 中的 LDAP 编程变得简单并跳过与您无关的章节(从 2006 年开始,但仍然可以)。文章中引用的Spring LDAP目前版本为 1.3.1。
If you want to go without Spring LDAP for now you can use the following traditional code:
如果您现在不想使用 Spring LDAP,您可以使用以下传统代码:
Map<String, String> env = new HashMap<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/dc=jayway,dc=se");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "uid="+ username +",ou=system"); // replace with user DN
env.put(Context.SECURITY_CREDENTIALS, password);
DirContext ctx;
try {
ctx = new InitialDirContext(env);
} catch (NamingException e) {
// handle
}
try {
SearchControls controls = new SearchControls();
controls.setSearchScope( SearchControls.SUBTREE_SCOPE);
ctx.search( "", "(objectclass=person)", controls);
// no need to process the results
} catch (NameNotFoundException e) {
// The base context was not found.
// Just clean up and exit.
} catch (NamingException e) {
// exception handling
} finally {
// close ctx or do Java 7 try-with-resources http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
}
回答by Naor Bar
Test the LDAP connection using Spring LDAP authentication:
使用 Spring LDAP 身份验证测试 LDAP 连接:
i.e. with the authenticate()method:
即使用authentication()方法:
ldapTemplate.authenticate(query, password);
or even better, with getContext()method:
甚至更好,使用getContext()方法:
ldapTemplate.getContextSource().getContext(userDn, userPassword));
Catch the org.springframework.ldap.CommunicationExceptionto check if the connection succeeds.
捕获org.springframework.ldap.CommunicationException以检查连接是否成功。
The full code snippet should look like this:
完整的代码片段应如下所示:
// Create the spring LdapTemplates; i.e. connections to the source and target ldaps:
try {
// Note: I'm using the direct LdapTemplate initialization rather than with bean creation (Spring ldap supports both)
log.info("Connecting to LDAP " + sourceHost + ":" + sourcePort + "...");
LdapContextSource sourceLdapCtx = new LdapContextSource();
sourceLdapCtx.setUrl("ldap://" + sourceHost + ":" + sourcePort + "/");
sourceLdapCtx.setUserDn(sourceBindAccount);
sourceLdapCtx.setPassword(sourcePassword);
sourceLdapCtx.setDirObjectFactory(DefaultDirObjectFactory.class);
sourceLdapCtx.afterPropertiesSet();
sourceLdapTemplate = new LdapTemplate(sourceLdapCtx);
// Authenticate:
sourceLdapTemplate.getContextSource().getContext(sourceBindAccount, sourcePassword);
} catch (Exception e) {
throw new Exception("Failed to connect to LDAP - " + e.getMessage(), e);
}
Note:I'm using spring LDAP 2.3.x version:
注意:我使用的是 spring LDAP 2.3.x 版本:
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
</dependency>