java 读取AD里User信息的objectSid

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 读取AD里User信息的objectSid相关的知识,希望对你有一定的参考价值。

AD域用户信息,读取 user的 objectSid为不可辨析的字符串。要如何处理才能为可辨析的。。在 C++中 有

ConvertSidToString(objectSid)这样的函数。Java中有么?

不可辨析的字符串,你确定是字符串???

一般不可辨析的应该不是字符串,是2进制或者16进制的字符,需要转换的。 用getByte(),先获得byte,然后再考虑转换成你需要的字符串。

这样的现象在网络中是很常见的,这只是我的一种推断,希望能够帮到你。
参考技术A 可以通过强砖就是类似这样(String)objectSid 或是通过toString()方法 objectSid.toString() 希望可以帮到你哦 参考技术B 取出之后,这个字段如果不为null。也可以转换成String的吧,String.valuof(AD)

java以及C#获取AD域上用户信息

JAVA


/**
 *  JAVA 读取AD用户信息
 *  aa00a00
 */
package com.wanda.sso.client.servlet;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

public class ADOperTest {

 public ADOperTest() {
 }

 public void GetADInfo() {
  String userName = "xueqiang"; // 用户名称
  String passwd = "123456";
  String host = "10.0.4.26"; // AD服务器
  String port = "389"; // 端口
  String domain = "@wanda-dev.cn"; // 邮箱的后缀名
  String url = new String("ldap://" + host + ":" + port);
  String user = userName.indexOf(domain) > 0 ? userName : userName
    + domain;
  Hashtable HashEnv = new Hashtable();
  // String adminName ="CN=oyxiaoyuanxy,CN=Users,DC=Hebmc,DC=com";//AD的用户名
  String adminName = "xueqiang"; // 注意用户名的写法:domain\User 或
  // [email protected]
  adminName = "xueqiang"; // 注意用户名的写法:domain\User 或 [email protected]
  String adminPassword = "123456"; // 密码
  HashEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别
  HashEnv.put(Context.SECURITY_PRINCIPAL, adminName); // AD User
  HashEnv.put(Context.SECURITY_CREDENTIALS, adminPassword); // AD Password
  HashEnv.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP工厂类
  HashEnv.put(Context.PROVIDER_URL, url);
  try {
   LdapContext ctx = new InitialLdapContext(HashEnv, null);
   // 域节点
   String searchBase = "DC=wanda-dev,DC=cn";
   // LDAP搜索过滤器类
   String searchFilter = "objectClass=User";
   // 搜索控制器
   SearchControls searchCtls = new SearchControls(); // Create the
   // search
   // controls
   // 创建搜索控制器
   searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Specify
   // the
   // search
   // scope
   // 设置搜索范围
   // searchCtls.setSearchScope(SearchControls.OBJECT_SCOPE); //
   // Specify the search scope 设置搜索范围
   String returnedAtts[] = { "memberOf", "distinguishedName",
     "Pwd-Last-Set", "User-Password", "cn" };// 定制返回属性
   // String returnedAtts[] = { "url", "whenChanged", "employeeID",
   // "name", "userPrincipalName", "physicalDeliveryOfficeName",
   // "departmentNumber", "telephoneNumber", "homePhone",
   // "mobile", "department", "sAMAccountName", "whenChanged",
   // "mail" }; // 定制返回属性
   searchCtls.setReturningAttributes(returnedAtts); // 设置返回属性集

   // 根据设置的域节点、过滤器类和搜索控制器搜索LDAP得到结果
   NamingEnumeration answer = ctx.search(searchBase, searchFilter,
     searchCtls);// Search for objects using the filter

   // 初始化搜索结果数为0
   int totalResults = 0;// Specify the attributes to return
   int rows = 0;

   while (answer.hasMoreElements()) {// 遍历结果集
    SearchResult sr = (SearchResult) answer.next();// 得到符合搜索条件的DN
    System.out.println(++rows
      + "************************************************");
    System.out.println(sr.getName());
    Attributes Attrs = sr.getAttributes();// 得到符合条件的属性集
    if (Attrs != null) {
     try {
      for (NamingEnumeration ne = Attrs.getAll(); ne
        .hasMore();) {
       Attribute Attr = (Attribute) ne.next();// 得到下一个属性
       System.out.println(" AttributeID=属性名:"
         + Attr.getID().toString());
       // 读取属性值
       for (NamingEnumeration e = Attr.getAll(); e
         .hasMore(); totalResults++) {
        System.out.println("    AttributeValues=属性值:"
          + e.next().toString());
       }
       System.out.println("    ---------------");
       // 读取属性值
       // Enumeration values = Attr.getAll();
       // if (values != null) { // 迭代
       // while (values.hasMoreElements()) {
       // System.out.println("    AttributeValues=属性值:"+
       // values.nextElement());
       // }
       // }
       // System.out.println("    ---------------");
      }
     } catch (NamingException e) {
      System.err.println("Throw Exception : " + e);
     }
    }
   }
   System.out
     .println("************************************************");
   System.out.println("Number: " + totalResults);
   ctx.close();
  } catch (NamingException e) {
   e.printStackTrace();
   System.err.println("Throw Exception : " + e);
  }
 }

 public static void main(String args[]) {
  // 实例化
  ADOperTest ad = new ADOperTest();
  ad.GetADInfo();
 }
}

 


 

C#

myLDAPPath = "LDAP://你的域名";

                    DirectoryEntry mySearchRoot = new DirectoryEntry(myLDAPPath);
                    DirectorySearcher myDirectorySearcher = new DirectorySearcher(mySearchRoot);

                    #region all informations

                    myDirectorySearcher.Filter = ("(objectClass=user)"); //user表示用户,group表示组
                    foreach (SearchResult mySearchResult in myDirectorySearcher.FindAll())
                    {
                         if (mySearchResult != null)
                         {
                              // Get the properties of the ‘mySearchResult‘.
                              ResultPropertyCollection myResultPropColl;
                              myResultPropColl = mySearchResult.Properties;
                              Console.WriteLine("The properties of the ‘mySearchResult‘ are :");

                              foreach (string myKey in myResultPropColl.PropertyNames)
                              {
                                   string tab = "    ";
                                   Console.WriteLine(myKey + " = ");
                                   foreach (Object myCollection in myResultPropColl[myKey])
                                   {
                                        Console.WriteLine(tab + myCollection);
                                   }
                              }
                              mySearchRoot.Dispose();
                         }
                    }

 


 

以上是关于java 读取AD里User信息的objectSid的主要内容,如果未能解决你的问题,请参考以下文章

如何check AD user的信息?

如何check AD user的域设置信息?

怎么用Java读取word文档里的内容格式信息,比如标题字体,颜色和段间距啥的?

AD更改登陆账户名后,后XenDesktop DDC无法同步用户信息

Java读取过去保存的变量

java里怎么把从数据库里读取的图片类型数据 显示到页面上啊 ?