操作AD的用户信息(通过LDAP应用)

  Windows AD是Windows ActiveDirectories的简称,意为活动目录。可以用这个服务,进行用户信息的管理。

       安装AD : http://blog.postcha.com/read/80

代码部分

 

//通过ad域登录
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        //ad域地址:windos server上输入ipconfig查看,369是固定端口,dc=contoso,dc=com是域的范围
        env.put(Context.PROVIDER_URL, "ldap://172.23.22.241:389");
        //ad域里面的用户
        env.put(Context.SECURITY_PRINCIPAL, "administrator@netfans.com");
        //ad域里面的密码
        env.put(Context.SECURITY_CREDENTIALS, "1qaz@WSX");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        DirContext ctx = null;
        NamingEnumeration results = null;
        String st="";
        
        try {
            //登录验证
            ctx = new InitialDirContext(env);
            SearchControls controls = new SearchControls();
            controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            //根据用户名查看ad域中是否存在当前用户
            //results = ctx.search("dc=netfans,dc=com", "(&(objectclass=person)(userprincipalname='administrator'))", controls);

            String lastSyncTime = "20190904085630.0Z";
            String syncFilter = String.format("(&(objectClass=user)(whenChanged>=%s)(whenCreated>=%s))", lastSyncTime, lastSyncTime);
            results = ctx.search("dc=netfans,dc=com", syncFilter, controls);
            SearchResult searchResult = (SearchResult) results.next();
            Attributes attributes = searchResult.getAttributes();
            System.out.println(new ObjectMapper().writeValueAsString(attributes));
        } catch (Exception e) {
            e.printStackTrace();
        }

 

  

posted @ 2019-09-05 16:31  喝花茶  阅读(588)  评论(0编辑  收藏  举报