【问题标题】:Connect to on Prem Microsoft Active Directory using Java使用 Java 连接到 on Prem Microsoft Active Directory
【发布时间】: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


【解决方案1】:

由于您已经在使用 spring,您可能想要关注 the spring guide to authenticating with LDAP

您应该知道的是,Active Directory 也是一个 LDAP 服务器。

LDAP 最大的问题是弄清楚所有参数应该是什么。

【讨论】:

  • 非常感谢...我能够连接到 AD 但无法验证用户身份...我收到 NamingException。我编辑了问题oo
  • 原来我使用了错误的参数。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多