This repository was archived by the owner on Jul 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathLdapClient.java
More file actions
170 lines (158 loc) · 5.52 KB
/
LdapClient.java
File metadata and controls
170 lines (158 loc) · 5.52 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* @ClassName: LdapClient
* @Description: TODO
* @Author: Summer
* @Date: 2021/8/2 11:00
* @Version: v1.0.0
* @Description:
**/
import java.util.Properties;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.naming.directory.*;
import javax.naming.ldap.*;
/**
* Created by baikai on 8/17/16.
*/
public class LdapClient {
private String ldapUrl;
private String ldapUserDN;
private String ldapPwd;
public LdapClient(String ldapUrl, String ldapUserDN, String ldapPwd){
this.ldapUrl = ldapUrl;
this.ldapUserDN = ldapUserDN;
this.ldapPwd = ldapPwd;
}
/**
* Create LDAP user
* @param userName
* @param password
* @param uidNumber
* @param gidNumber
*/
public void createLDAPUser(String userName, String password, String uidNumber, String gidNumber){
LdapContext context = this.initLDAPContext();
Attributes matchAttrs = new BasicAttributes(true);
BasicAttribute objclassSet = new BasicAttribute("objectClass");
objclassSet.add("account");
objclassSet.add("posixAccount");
matchAttrs.put(objclassSet);
matchAttrs.put(new BasicAttribute("uid", userName));
matchAttrs.put(new BasicAttribute("cn", userName));
matchAttrs.put(new BasicAttribute("uidNumber", uidNumber));
matchAttrs.put(new BasicAttribute("gidNumber", gidNumber));
matchAttrs.put(new BasicAttribute("homeDirectory", "/home/" + userName));
matchAttrs.put(new BasicAttribute("userpassword", password));
matchAttrs.put(new BasicAttribute("description", "LDAP user."));
try {
context.bind("uid=" + userName + ",ou=People,dc=asiainfo,dc=com", null, matchAttrs);
} catch (NamingException e) {
e.printStackTrace();
}finally {
this.closeLdapContext(context);
}
}
/**
* Create LDAP user group
* @param groupName
* @param password
* @param gidNumber
*/
public void createLDAPUserGroup(String groupName, String password, String gidNumber){
LdapContext context = this.initLDAPContext();
Attributes matchAttrs = new BasicAttributes(true);
matchAttrs.put(new BasicAttribute("objectclass", "posixGroup"));
matchAttrs.put(new BasicAttribute("cn", groupName));
matchAttrs.put(new BasicAttribute("gidNumber", gidNumber));
matchAttrs.put(new BasicAttribute("userPassword", password));
try {
context.bind("cn=" + groupName + ",ou=People,dc=asiainfo,dc=com", null, matchAttrs);
} catch (NamingException e) {
e.printStackTrace();
}finally {
this.closeLdapContext(context);
}
}
/**
* Delete LDAP user
* @param userName
*/
public void deleteLDAPUser(String userName){
LdapContext context = this.initLDAPContext();
try {
context.unbind(userName);
} catch (NamingException e) {
e.printStackTrace();
}finally {
this.closeLdapContext(context);
}
}
/**
* Delete LDAP user group
* @param groupName
*/
public void deleteLDAPUserGroup(String groupName){
this.deleteLDAPUser(groupName);
}
/**
* Modify LDAP user attribute with new value
* @param userName
* @param attributeName
* @param attributeNewValue
*/
public void updateLDAPUserAttribute(String userName, String attributeName, String attributeNewValue){
LdapContext context = this.initLDAPContext();
ModificationItem[] mods = new ModificationItem[1];
mods[0] = new ModificationItem(context.REPLACE_ATTRIBUTE, new BasicAttribute(attributeName, attributeNewValue));
try{
context.modifyAttributes(userName, mods);
}catch (NamingException e) {
e.printStackTrace();
}finally {
this.closeLdapContext(context);
}
}
/**
* Search LDAP users by user dn and filter
* @param userName
* @param filter
* @return NamingEnumeration<SearchResult>
*/
public NamingEnumeration<SearchResult> searchLDAPUser(String userName, String filter){
NamingEnumeration<SearchResult> searchResults = null;
LdapContext context = this.initLDAPContext();
SearchControls ctrl = new SearchControls();
ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
try {
searchResults = context.search(userName, filter, ctrl);
} catch (NamingException e) {
e.printStackTrace();
}finally {
this.closeLdapContext(context);
}
return searchResults;
}
private LdapContext initLDAPContext(){
LdapContext context = null;
Properties mEnv = new Properties();
mEnv.put(LdapContext.AUTHORITATIVE, "true");
mEnv.put(LdapContext.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
mEnv.put(LdapContext.PROVIDER_URL, this.ldapUrl);
mEnv.put(LdapContext.SECURITY_AUTHENTICATION, "simple");
mEnv.put(LdapContext.SECURITY_PRINCIPAL, this.ldapUserDN);
mEnv.put(LdapContext.SECURITY_CREDENTIALS, this.ldapPwd);
try {
context = new InitialLdapContext(mEnv,null);
} catch (NamingException e) {
e.printStackTrace();
}
return context;
}
private void closeLdapContext(LdapContext context){
try {
context.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
}