springLdap 操做 ad域例子(应该比较详细)

一、验证管理员帐户

    好比域以下: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)处处证书

    待更新

相关文章
相关标签/搜索