您的位置:首页 > 编程语言 > Java开发

Java如何获取ldap中的sid

2015-09-24 16:56 411 查看
(版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。)

一、对SID的初步认识
  SID即安全标识符(System IDentifier),它用来标识用户身份的。当系统每次创建用户都会分配一个唯一的SID。正因为SID有这样的特性,从Windows 2000以后的W系统对SID的依赖性较高,包括很多系统应用在内的系统内部进程引用帐户的SID而不是帐户的用户名和组名。因为用户的登陆名、显示名和归属的组等都可以修改,将一个帐户删除后再建立一个同名帐户,该账户的SID不同于被删除的那个帐户,所以它也不具有授权给前一个帐户的权利和权限。

二、系统如何使用SID
  当用户通过输入用户名口令得到身份验证(authentication)后,系统内部进程会给用户发放一个访问令牌,其实也就相当于一个票证(ticket)。此后用户访问系统资源时不再需要提供用户名和口令,只需要将访问令牌提供给系统,然后系统检查用户试图访问对象(Resources)上的访问控制列表(ACL)。如果用户被允许访问该对象,系统将会分配给用户相应的访问权限,这也就完成了授权(authorization)的过程。
  上面描述的其实就是一个authentication & authorization的过程,那么如果系统修改了Resources ACL则需要等待ticket过期更换或者用户注销再登陆重新authentication获得新的ticket。
 

三、SID号码如何组成
  例:S-1-5-21-2062728589-3024857285-4110131050-500
  上面着一串是一个标准的SID。第一项S标示该字符串是SID;第二项是SID的版本号,这里版本号是1;第三项十标志符的颁发机构,Win2000之后的颁发机构都是5;最后是一系列资颁发机构前面3组是标志域,最后一组是标志域内的帐户和组。例子中的SID最后一段标志位是500,这代表了它是一个系统内置管理员帐号administrator的SID,又比如最后一段是501的话则是代表GUEST的帐号。有很多内置用户和组的SID相应标志位都是固定的,对应关系这里不再赘述。

四、如何获得用户和主机的SID
1、PsGetSid
  PsGetSid是PsTools工具集中,由sysinternals发布,下载地址:http://download.sysinternals.com/Files/PsTools.zip 

2、在控制台输入 whoami /all

五、Java代码的实现部分

 

 Java Code 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

 

/**
 * @Description:获取AD域账户信息中的objectSid,并且将byte[]转化为String
 * @ClassName: Test
 * @Copyright: Copyright (c) 2015
 * @author CUG_ZG
 * @date 2015-9-23 下午15:22:35
 * @version V1.0
 */
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
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 Test

{

    public String getObjectSid(Attributes attrs) throws NamingException

    {

        if (attrs.get("objectSid") != null)

        {

            Object object = attrs.get("objectSid").get();

            byte[] SID = (byte[]) object;

            // byte[] SID = object.toString().getBytes();//不可以用这种转换类型,有损耗
            StringBuilder strSID = new StringBuilder("S-");

            strSID.append(SID[0]).append('-');

            // bytes[2..7] :
            StringBuilder tmpBuff = new StringBuilder();

            for (int t = 2; t <= 7; t++)

            {

                String hexString = Integer.toHexString(SID[t] & 0xFF);

                tmpBuff.append(hexString);

            }

            strSID.append(Long.parseLong(tmpBuff.toString(), 16));

            // bytes[1] : the sub authorities count
            int count = SID[1];

            // bytes[8..end] : the sub authorities (these are Integers - notice
            // the endian)
            for (int i = 0; i < count; i++)

            {

                int currSubAuthOffset = i * 4;

                tmpBuff.setLength(0);

                tmpBuff.append(String.format("%02X%02X%02X%02X",

                                             (SID[11 + currSubAuthOffset] & 0xFF),

                                             (SID[10 + currSubAuthOffset] & 0xFF),

                                             (SID[9 + currSubAuthOffset] & 0xFF),

                                             (SID[8 + currSubAuthOffset] & 0xFF)));

                strSID.append('-').append(

                    Long.parseLong(tmpBuff.toString(), 16));

            }

            return strSID.toString();

        }

        return null;

    }

    public static void main(String[] args)

    {

        Test test = new Test();

        Properties env = new Properties();

        String adminName = "username@domain.com";// username@domain
        String adminPassword = "password";// password
        String ldapURL = "LDAP://ipaddress:port";// ip:port
        env.put(Context.INITIAL_CONTEXT_FACTORY,

                "com.sun.jndi.ldap.LdapCtxFactory");

        env.put(Context.SECURITY_AUTHENTICATION, "simple");// "none","simple","strong"
        env.put(Context.SECURITY_PRINCIPAL, adminName);

        env.put(Context.SECURITY_CREDENTIALS, adminPassword);

        env.put(Context.PROVIDER_URL, ldapURL);

        //加上这个可以避免在byte[]转化为String类型的编译错误
        env.put("java.naming.ldap.attributes.binary", "objectSid");

        try

        {

            LdapContext ctx = new InitialLdapContext(env, null);

            SearchControls searchCtls = new SearchControls();

            searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

            String searchFilter = "(&(objectCategory=person)(objectClass=user)(name=*))";

            String searchBase = "DC=mosscenterm,DC=com";

            String returnedAtts[] = { "objectSid" };

            searchCtls.setReturningAttributes(returnedAtts);

            NamingEnumeration<SearchResult> answer = ctx.search(searchBase,

                    searchFilter, searchCtls);

            while (answer.hasMoreElements())

            {

                SearchResult sr = (SearchResult) answer.next();

                Attributes attrs = sr.getAttributes();

                System.out.println("<<<::[" + sr.getNameInNamespace()

                                   + "]::>>>>");

                System.out.println("<<<::[" + test.getObjectSid(attrs)

                                   + "]::>>>>");

            }

            ctx.close();

        }

        catch (NamingException e)

        {

            e.printStackTrace();

            System.err.println("Problem searching directory: " + e);

        }

    }

}

 

 

 参考文章:

1.Java/JNDI: How to Convert Active Directory ObjectSID into String Format  Author: Eyal Lupu

From:  http://www.jroller.com/eyallupu/entry/java_jndi_how_to_convert


2.详解SID之终结篇  作者:梦卿的砖头

From:  http://www.360doc.com/content/08/1201/14/74060_2029807.shtml






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java sid objectSid ldap