我有一个简单的任务,使用Java对Active Directory进行身份验证.只是验证凭据而不是其他任何内容.假设我的域名为"fun.xyz.tld",OU路径未知,用户名/密码为testu/testp.
我知道有一些Java库可以简化这项任务,但我没有成功实现它们.我发现的大多数示例都是针对LDAP的,而不是特定的Active Directory.发出LDAP请求意味着在其中发送OU路径,这是我没有的.此外,发出LDAP请求的应用程序应该已绑定到Active Directory以便访问它...不安全,因为凭据必须存储在某处可被发现的位置.如果可能的话,我希望测试绑定测试凭据 - 这意味着帐户有效.
最后,如果可能的话,有没有办法使这种认证机制加密?我知道AD使用Kerberos,但不确定Java的LDAP方法是否可以.
有没有人有一个工作代码的例子?谢谢.
我正在玩LDAP和Java搜索.这是我的LDIF导出与一个简单的组织
version: 1
dn: dc=example,dc=com
objectClass: organization
objectClass: dcObject
objectClass: top
dc: example
o: MyOrganization
description: Test Description
dn: ou=people, dc=example,dc=com
objectClass: organizationalUnit
objectClass: top
ou: people
description: All users in demo company
dn: cn=Johnny Doe,ou=people,dc=example,dc=com
objectClass: organizationalPerson
objectClass: person
objectClass: inetOrgPerson
objectClass: top
cn: Johnny Doe
sn: Johnny
homephone: 123-456-7890
mail: johnny@johnny.com
ou: Development
uid: jjohnny
userpassword:: johnny
dn: cn=Samuel Johnson,ou=people,dc=example,dc=com
objectClass: organizationalPerson
objectClass: person
objectClass: inetOrgPerson
objectClass: top
cn: Samuel Johnson
sn: Samuel
homephone: 123-456-7890
mail: sam@ssam.com
ou: …Run Code Online (Sandbox Code Playgroud) 我需要通过SSL连接到LDAP目录.
在非生产环境中,我们使用自签名证书,当然,这些证书无法通过以下方式验证:
javax.naming.CommunicationException: simple bind failed: ldapserver:636 [Root exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target]
at com.sun.jndi.ldap.LdapClient.authenticate(LdapClient.java:197)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2694)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:293)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:175)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:193)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:136)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:66)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:134)
Run Code Online (Sandbox Code Playgroud)
我知道如何使用自定义信任管理器进行启用SSL的连接,但不知道如何使用与JNDI API连接的连接,我不管理实际的连接.也就是说,以下标准设置在哪里可以插入信任管理器?
提前致谢.
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldaps://ldapserver:636");
env.put(Context.SECURITY_PROTOCOL, "ssl");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "myUser");
env.put(Context.SECURITY_CREDENTIALS, "myPassword");
LdapContext ctx = new InitialLdapContext(env, null);
ctx.search (...)
Run Code Online (Sandbox Code Playgroud) 我无法使用LDAP对用户进行身份验证.我有以下细节:
URL=ldap://10.10.10.10:389
LDAP BASE:DC=lab2,DC=ins
LDAP Bind Account: CN=Ldap Bind,OU=Service Accounts,OU=TECH,DC=lab2,DC=ins
LDAP Bind Account Pw: secret
Run Code Online (Sandbox Code Playgroud)
我可以sAMAccountName使用上面的详细信息搜索值,但是如何使用用户名和密码验证用户?
如果您按照我之前的问题进行操作,那么您将了解到,我已成功连接到LDAP服务器但无法对其进行身份验证.
用户进行身份验证:
user: someusername
password: somepwd
Run Code Online (Sandbox Code Playgroud)
我无法连接到LDAP服务器,'somepwd'我应该如何使用someusername.我能够搜索给定的用户sAMAccountName.