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

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

2018-03-01 00:00 134 查看
摘要: 这个操作ad域的活有点操蛋啊,碰到太多问题,很多看着官方文档也会各种报错,慢慢更新中~

1、验证管理员账户

比如域如下:



则用户名就得写成: Administrator@sendo.com (Administrator是window server的管理员账户)

/**
* 验证账户
* @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;
}
}


2、验证其他用户

验证其他用户其实也可以按照验证管理员那样,包括验证管理员,照样也可以这样验证,只是验证之前,需要先获取这个ldapTemplate,而获取这个实例还是得先通过验证管理员账户,所以当拿到这个实例,后面验证普通用户就比较推荐用这种方法验证,比较简洁。

ps: 由于我的域名叫 sendo.com ,所以下面的cs.setBase传入的就是"dc=sendo,dc=com",同样,如果你的域名叫:hello.world.com,则传入的base就是"dc=hello,dc=world,dc=com"

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);
}
}


3、查询

查询比较简单,列举一个我自己爱用的

public static List<Person> search() {
List<Person> personList = ldapTemplate.search(query().where("objectclass").is("user"), new PersonMapper());
return personList;
}

新建一个PersonMapper类,将属性传给自己定义的实例。

/**
* 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;
}
}


4、增加用户(包括启用用户以及禁用用户)、用户组



如果是上面这个用户,则创建Name的时候,要按照从小到大的顺序cn=yushengfan,cn=Users

如果是上面这个名为234的组织单元,则写成cn=某某某,ou=234

创建用户组和创建用户一样,只是objectclass类型为{"top", "group"} (groups还是group 忘记了)

ps:有一个很重要的属性userAccountControl (用户是否启用)

如 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);
}


5、修改用户、用户组

要修改别的属性,多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);
}


6、删除用户/用户组

/**
* 删除用户
* @param name
*/
public static void delete() {
Name userDn = LdapNameBuilder.newInstance("cn=zhangting,ou=234").build();
ldapTemplate.unbind(dn);
}


7、添加用户到组



组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属性的值而已

8、移出组内用户

和添加组相反

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("");
}


9、修改密码

1)添加证书

2)到处证书

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