使用 Java 连接到 Prem Microsoft Active Directory
Connect to on Prem Microsoft Active Directory using Java
我是新手,正在尝试使用 Java spring 启动连接到 Prem Microsoft Active Directory。
Active Directory 部署在远程服务器上,我正在使用 Microsoft 远程桌面访问该服务器。有什么方法可以建立连接并对用户进行身份验证吗?
更新:
我能够连接到 AD,但出现 NamingException
String username = "test@myid.com.local";
String password = "Yahoo@1234";
String url = "ldap://100.36.224.125:389/dc=springframework,dc=org";
String base = "ou=people,dc=example,dc=com";
Hashtable<String, Object> ldapParams = new Hashtable<String, Object>();
ldapParams.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
ldapParams.put(Context.PROVIDER_URL, url);
ldapParams.put(Context.SECURITY_AUTHENTICATION, "simple");
ldapParams.put(Context.SECURITY_PRINCIPAL, username);
ldapParams.put(Context.SECURITY_CREDENTIALS, password);
// Specify SSL
//ldapParams.put(Context.SECURITY_PROTOCOL, "ssl");
InitialDirContext ldapCtx = null;
try {
ldapCtx = new InitialDirContext(ldapParams);
System.out.println(ldapCtx);
if (ldapCtx != null) {
System.out.println("login success.");
String searchFilter = "(cn=itadmin)";
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = ldapCtx.search(base, searchFilter, controls);
while (results.hasMore()) {
SearchResult searchResult = (SearchResult) results.next();
Attributes attributes = searchResult.getAttributes();
Attribute attr = attributes.get("cn");
String cn = (String) attr.get();
System.out.println(" Person Common Name = " + cn);
}
}
} catch (AuthenticationException ex) {
System.out.println("login fail. [err 1]");
System.err.println(ex);
} catch (NamingException ex) {
System.out.println("login fail. [err 2]");
System.err.println(ex);
} catch (Exception e) {
System.out.println("login fail. [err 3]");
System.err.println(e);
} finally {
System.out.println("LDAP Context is " + ldapCtx);
}
因为您已经在使用 spring,所以您可能想要关注 the spring guide to authenticating with LDAP。
您应该知道,Active Directory 也是一个 LDAP 服务器。
LDAP 最大的问题是弄清楚所有参数应该是什么。
我是新手,正在尝试使用 Java spring 启动连接到 Prem Microsoft Active Directory。 Active Directory 部署在远程服务器上,我正在使用 Microsoft 远程桌面访问该服务器。有什么方法可以建立连接并对用户进行身份验证吗?
更新:
我能够连接到 AD,但出现 NamingException
String username = "test@myid.com.local";
String password = "Yahoo@1234";
String url = "ldap://100.36.224.125:389/dc=springframework,dc=org";
String base = "ou=people,dc=example,dc=com";
Hashtable<String, Object> ldapParams = new Hashtable<String, Object>();
ldapParams.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
ldapParams.put(Context.PROVIDER_URL, url);
ldapParams.put(Context.SECURITY_AUTHENTICATION, "simple");
ldapParams.put(Context.SECURITY_PRINCIPAL, username);
ldapParams.put(Context.SECURITY_CREDENTIALS, password);
// Specify SSL
//ldapParams.put(Context.SECURITY_PROTOCOL, "ssl");
InitialDirContext ldapCtx = null;
try {
ldapCtx = new InitialDirContext(ldapParams);
System.out.println(ldapCtx);
if (ldapCtx != null) {
System.out.println("login success.");
String searchFilter = "(cn=itadmin)";
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = ldapCtx.search(base, searchFilter, controls);
while (results.hasMore()) {
SearchResult searchResult = (SearchResult) results.next();
Attributes attributes = searchResult.getAttributes();
Attribute attr = attributes.get("cn");
String cn = (String) attr.get();
System.out.println(" Person Common Name = " + cn);
}
}
} catch (AuthenticationException ex) {
System.out.println("login fail. [err 1]");
System.err.println(ex);
} catch (NamingException ex) {
System.out.println("login fail. [err 2]");
System.err.println(ex);
} catch (Exception e) {
System.out.println("login fail. [err 3]");
System.err.println(e);
} finally {
System.out.println("LDAP Context is " + ldapCtx);
}
因为您已经在使用 spring,所以您可能想要关注 the spring guide to authenticating with LDAP。
您应该知道,Active Directory 也是一个 LDAP 服务器。
LDAP 最大的问题是弄清楚所有参数应该是什么。