好比域以下:java
則用戶名就得寫成: Administrator@sendo.com (Administrator是window server的管理員帳戶)app
/** * 驗證帳戶 * @param name * @param password * @return */ public static boolean check(String name, String password) { LdapContext dc = null; Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://192.168.254.147:389");//域ip和端口號 env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "Administrator@sendo.com"); // 登陸名 env.put(Context.SECURITY_CREDENTIALS, "Ysfan910628"); env.put(Context.REFERRAL, "throw"); env.put("java.naming.ldap.attributes.binary", "objectGUID");// objectGUID也能夠指定爲其它屬性 try { DirContext ctx = new InitialDirContext(env); System.out.println("認證成功"); ctx.close(); return true; } catch (Exception e) { System.out.println("認證失敗"); return false; } }
驗證其餘用戶其實也能夠按照驗證管理員那樣,包括驗證管理員,照樣也能夠這樣驗證,只是驗證以前,須要先獲取這個ldapTemplate,而獲取這個實例仍是得先經過驗證管理員帳戶,因此當拿到這個實例,後面驗證普通用戶就比較推薦用這種方法驗證,比較簡潔。ui
ps: 因爲個人域名叫 sendo.com ,因此下面的cs.setBase傳入的就是"dc=sendo,dc=com",一樣,若是你的域名叫:hello.world.com,則傳入的base就是"dc=hello,dc=world,dc=com"spa
public static LdapTemplate ldapTemplate = null; public static final String LDAP_URL = "ldap://192.168.254.147:389"; public static void init() { LdapContextSource cs = new LdapContextSource(); cs.setCacheEnvironmentProperties(false); cs.setUrl(LDAP_URL); cs.setBase("dc=sendo,dc=com"); cs.setUserDn("Administrator@sendo.com"); cs.setPassword("Ysfan910628"); cs.afterPropertiesSet(); ldapTemplate = new LdapTemplate(cs); ldapTemplate.setIgnorePartialResultException(true); } /** * 驗證用戶名密碼 * @param userName 好比帳戶是 yushengfan@sendo.com userName是yushengfan 和驗證管理員用戶不同 * @param password */ public static void authenticate(String userName, String password) { DirContext ctx = null; try { ctx = ldapTemplate.getContextSource().getContext(userName, password); System.out.println("密碼驗證成功!"); } catch (Exception e) { System.out.println("密碼驗證失敗!"); } finally { LdapUtils.closeContext(ctx); } }
查詢比較簡單,列舉一個我本身愛用的code
public static List<Person> search() { List<Person> personList = ldapTemplate.search(query().where("objectclass").is("user"), new PersonMapper()); return personList; }
新建一個PersonMapper類,將屬性傳給本身定義的實例。 server
/** * Created by sendo on 01/03/2018. */ public class PersonMapper implements ContextMapper<Person> { public Person mapFromContext(Object ctx) throws NamingException { Person person = new Person(); DirContextAdapter context = (DirContextAdapter)ctx; person.setCn(context.getStringAttribute("cn")); person.setCompany(context.getStringAttribute("company")); person.setDescription(context.getStringAttribute("description")); person.setGivenname(context.getStringAttribute("givenname")); person.setObjectclass(context.getStringAttributes("objectclass")); person.setSn(context.getStringAttribute("sn")); person.setSamaccountname(context.getStringAttribute("samaccountname")); person.setUseraccountcontrol(context.getStringAttribute("useraccountcontrol")); person.setUserprincipalname(context.getStringAttribute("userprincipalname")); person.setDn(context.getDn().toString()); person.setDistinguishedName(context.getStringAttribute("distinguishedname")); return person; } }
若是是上面這個用戶,則建立Name的時候,要按照從小到大的順序cn=yushengfan,cn=Usersxml
若是是上面這個名爲234的組織單元,則寫成cn=某某某,ou=234ip
建立用戶組和建立用戶同樣,只是objectclass類型爲{"top", "group"} (groups仍是group 忘記了)ci
ps:有一個很重要的屬性userAccountControl (用戶是否啓用) rem
如 context.setAttributeValue("userAccountControl", "514") // 514禁用 512啓用 (暫時發現544也能夠啓用)
/** * 建立用戶(比較推薦) * @param name */ public static void create2(String name) { Name userDn = LdapNameBuilder.newInstance("cn=yushengfan,cn=Users").build(); DirContextAdapter context = new DirContextAdapter(userDn); context.setAttributeValues("objectclass", new String[] {"top", "person", "user"}); context.setAttributeValue("sn", "testsn"); context.setAttributeValue("description", "description"); ldapTemplate.bind(context); }
要修改別的屬性,多set一些就成
/** * 更新用戶 * @param name */ public static void update2() { Name userDn = LdapNameBuilder.newInstance("cn=zhangting,ou=234").build(); DirContextOperations context = ldapTemplate.lookupContext(userDn); context.setAttributeValue("userAccountControl", "544"); ldapTemplate.modifyAttributes(context); }
/** * 刪除用戶 * @param name */ public static void delete() { Name userDn = LdapNameBuilder.newInstance("cn=zhangting,ou=234").build(); ldapTemplate.unbind(dn); }
組blancat在Users下, dn爲 cn=blancat,cn=Users
用戶zhangting在組織單元爲234的目錄下,dn就爲: cn=zhangting,ou=234
將zhangting移入到組blancat下,則代碼以下:
/** * 增長成員到組 */ public static void addMemberToGroup() { Name groupDn = LdapNameBuilder.newInstance("cn=blancat,cn=Users").build(); // 組的dn Name userDn = LdapNameBuilder.newInstance("cn=zhangting,ou=234").build(); // 成員的dn DirContextOperations ctxGroup = ldapTemplate.lookupContext(groupDn); DirContextOperations ctxUser = ldapTemplate.lookupContext(userDn); ctxGroup.addAttributeValue("member", ctxUser.getStringAttribute("distinguishedname")); ldapTemplate.modifyAttributes(ctxGroup); System.out.print(""); }
其實就是修改member屬性的值而已
和添加組相反
public static void removeMemberToGroup() { Name groupDn = LdapNameBuilder.newInstance("cn=blancat,cn=Users").build(); // 按照從小到大的順序 Name userDn = LdapNameBuilder.newInstance("cn=zhangting,ou=234").build(); DirContextOperations ctxGroup = ldapTemplate.lookupContext(groupDn); DirContextOperations ctxUser = ldapTemplate.lookupContext(userDn); ctxGroup.removeAttributeValue("member", ctxUser.getStringAttribute("distinguishedname")); ldapTemplate.modifyAttributes(ctxGroup); System.out.print(""); }
1)添加證書
2)處處證書
待更新