【发布时间】:2021-08-22 11:36:20
【问题描述】:
我是新手,正在尝试使用 Java spring boot 连接到 on 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);
}
【问题讨论】:
-
您是否使用任何特定的广告框架,您的程序的目的是什么?您使用远程桌面的事实与您的问题无关。
-
感谢您的回复。目的是从 AD 对我的应用程序上的用户进行身份验证。我正在使用 Microsoft 活动目录
标签: java spring-boot active-directory ldap