-
LDAP
- package com.smnpc.util;
-
- import java.util.Hashtable;
- import java.util.Vector;
-
- 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.BasicAttribute;
- import javax.naming.directory.BasicAttributes;
- import javax.naming.directory.DirContext;
- import javax.naming.directory.InitialDirContext;
- import javax.naming.directory.ModificationItem;
- import javax.naming.directory.SearchControls;
- import javax.naming.directory.SearchResult;
- import javax.naming.ldap.LdapContext;
-
-
- public class LdapbyUser {
- DirContext dc = null;
- String root = "dc=example,dc=com";
-
- public LdapbyUser(String dn,String employeeID) {
- init();
- modifyInformation(dn,employeeID);
- close();
- }
-
- public void init() {
- Hashtable env = new Hashtable();
- String LDAP_URL = "ldap://xxxx:389"; // LDAP访问地址
- String adminName = "example\user";
- String adminPassword = "userpassword";
- env.put(Context.INITIAL_CONTEXT_FACTORY,
- "com.sun.jndi.ldap.LdapCtxFactory");
- env.put(Context.PROVIDER_URL, LDAP_URL);
- env.put(Context.SECURITY_AUTHENTICATION, "simple");
- env.put(Context.SECURITY_PRINCIPAL, adminName);
- env.put(Context.SECURITY_CREDENTIALS, adminPassword);
- try {
- dc = new InitialDirContext(env);
- System.out.println("认证成功");
- } catch (javax.naming.AuthenticationException e) {
- System.out.println("认证失败");
- } catch (Exception e) {
- System.out.println("认证出错:" + e);
- }
- }
-
- public void add(String newUserName) {
- try {
- BasicAttributes attrs = new BasicAttributes();
- BasicAttribute objclassSet = new BasicAttribute("objectClass");
- objclassSet.add("sAMAccountName");
- objclassSet.add("employeeID");
- attrs.put(objclassSet);
- attrs.put("ou", newUserName);
- dc.createSubcontext("ou=" + newUserName + "," + root, attrs);
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("Exception in add():" + e);
- }
- }
-
- public void delete(String dn) {
- try {
- dc.destroySubcontext(dn);
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("Exception in delete():" + e);
- }
- }
-
- public boolean renameEntry(String oldDN, String newDN) {
- try {
- dc.rename(oldDN, newDN);
- return true;
- } catch (NamingException ne) {
- System.err.println("Error: " + ne.getMessage());
- return false;
- }
- }
-
- public boolean modifyInformation(String dn,String employeeID) {
- try {
- System.out.println("updating...
");
- ModificationItem[] mods = new ModificationItem[1];
-
-
- Attribute attr0 = new BasicAttribute("employeeID",employeeID);
- mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attr0);
- dc.modifyAttributes(dn+",dc=example,dc=com", mods);
- return true;
- } catch (NamingException e) {
- e.printStackTrace();
- System.err.println("Error: " + e.getMessage());
- return false;
- }
- }
-
- public void close() {
- if (dc != null) {
- try {
- dc.close();
- } catch (NamingException e) {
- System.out.println("NamingException in close():" + e);
- }
- }
- }
-
- public void searchInformation(String base, String scope, String filter) {
- SearchControls sc = new SearchControls();
- if (scope.equals("base")) {
- sc.setSearchScope(SearchControls.OBJECT_SCOPE);
- } else if (scope.equals("one")) {
- sc.setSearchScope(SearchControls.ONELEVEL_SCOPE);
- } else {
- sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
- }
- NamingEnumeration ne = null;
- try {
- ne = dc.search(base, filter, sc);
- while (ne.hasMore()) {
- System.out.println();
- SearchResult sr = (SearchResult) ne.next();
- String name = sr.getName();
- if (base != null && !base.equals("")) {
- System.out.println("entry: " + name + "," + base);
- } else {
- System.out.println("entry: " + name);
- }
-
- Attributes at = sr.getAttributes();
- NamingEnumeration ane = at.getAll();
- while (ane.hasMore()) {
- Attribute attr = (Attribute) ane.next();
- String attrType = attr.getID();
- NamingEnumeration values = attr.getAll();
- Vector vals = new Vector();
- while (values.hasMore()) {
- Object oneVal = values.nextElement();
- if (oneVal instanceof String) {
- System.out.println(attrType + ": " + (String) oneVal);
- } else {
- System.out.println(attrType + ": " + new String((byte[]) oneVal));
- }
- }
- }
- }
- } catch (Exception nex) {
- System.err.println("Error: " + nex.getMessage());
- nex.printStackTrace();
- }
- }
- public void Ldapbyuserinfo(String userName) {
- SearchControls searchCtls = new SearchControls();
- searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
- String searchFilter = "sAMAccountName=" + userName;
- String searchBase = "DC=example,DC=COM";
- int totalResults = 0;
- String returnedAtts[] = { "url", "whenChanged", "employeeID", "name",
- "userPrincipalName", "physicalDeliveryOfficeName",
- "departmentNumber", "telephoneNumber", "homePhone", "mobile",
- "department", "sAMAccountName", "whenChanged", "mail" };
-
- searchCtls.setReturningAttributes(returnedAtts);
-
-
- try {
- NamingEnumeration answer = dc.search(searchBase, searchFilter,
- searchCtls);
- if (answer == null || answer.equals(null)) {
- System.out.println("answer is null");
- } else {
- System.out.println("answer not null");
- }
- while (answer.hasMoreElements()) {
- SearchResult sr = (SearchResult) answer.next();
- System.out
- .println("************************************************");
- System.out.println("getname=" + 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++) {
- String user = e.next().toString();
- System.out.println(user);
- }
- }
- } catch (NamingException e) {
- System.err.println("Throw Exception : " + e);
- }
- }
- }
- System.out.println("Number: " + totalResults);
- } catch (Exception e) {
- e.printStackTrace();
- System.err.println("Throw Exception : " + e);
- }
- }
-
- public static void main(String[] args) {
- new LdapbyUser("CN=RyanHanson","bbs.it-home.org");
- }
- }
-
相关阅读:
数据结构问题集锦
大作业 开源项目列表
数据结构问题集锦
leetcode174
leetcode152
经典算法之KMP
给出一个字符串,将其每一个字符表示成16进制表示,要求每个十六进制为8位数
作业
ASP 作业题
ASP.NET 作业题
-
原文地址:https://www.cnblogs.com/markleilei/p/4796154.html
Copyright © 2020-2023
润新知