JAVA设置AD域用户密码

该博客介绍了如何在LDAP环境中创建、修改和验证用户密码。通过Java代码展示了设置密码的加密过程,以及使用DirContextOperations进行属性修改,包括密码更新。此外,还提供了登录验证的实现方法。

1.创建用户

设置密码时,重点是密码的字段名为unicodePwd,密码需进行加密

context.setAttributeValue(“unicodePwd”, createUnicodePassword(password));//添加密码

	@Override
    public boolean save(String username, String loginName, String password) {
        try {
            Name userDn = LdapNameBuilder.newInstance("CN=" + username + ",OU=测试用户,OU=test").build();
            DirContextAdapter context = new DirContextAdapter(userDn);
            context.setAttributeValues("objectclass", new String[]{"top", "person", "user"});
            context.setAttributeValue("displayName", username);
            context.setAttributeValue("description", "测试用户");
            context.setAttributeValue("unicodePwd", createUnicodePassword(password));//添加密码
            context.setAttributeValue("telephoneNumber", "1234567891");
            context.setAttributeValue("userAccountControl", "512");   //当前账号是否被禁用(514禁用  512启用 默认禁用)
            context.setAttributeValue("userPrincipalName", String.format(domainName, loginName));
            context.setAttributeValue("sAMAccountName", loginName);
            ldapTemplate.bind(context);
            log.info("新增AD域用户成功:{}", username);
            return true;
        } catch (Exception e) {
            //e.printStackTrace();
            log.info("新增AD域用户{}org=【{}】失败原因:{}", username, loginName, e.getMessage());
            return false;
        }
    }
    public byte[] createUnicodePassword(String password){
        String quotedPassword = "\"" + password + "\"";
        byte[] newUnicodePassword = quotedPassword.getBytes(StandardCharsets.UTF_16LE);
        return newUnicodePassword;

    }

2.修改密码

	@Override
    public boolean updatePwd(String pwd){
        Name userDn = LdapNameBuilder.newInstance("CN=" + username + ",OU=测试用户,OU=test").build();
        ModificationItem[] mods = new ModificationItem[1];
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("unicodePwd", createUnicodePassword(pwd)));
        try {
            ldapTemplate.modifyAttributes(userDn, mods);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }
    }

可以用下面的方法进行修改(此方法还可便捷的进行其他属性的修改)

        DirContextOperations ctxUser = ldapTemplate.lookupContext("CN=" + username + ",OU=测试用户,OU=test");
        ctxUser.setAttributeValue("unicodePwd", createUnicodePassword(pwd));
        ldapTemplate.modifyAttributes(ctxUser);

3.验证

可通过以下方法来进行登录验证

    @Value("${spring.ldap.base:DC=kittlen,DC=com}")
    private String base;

    @Override
    public boolean login(String userName, String passWord) {
    	String dn="CN=" + userName+ ",OU=测试用户,OU=test"
        String userDomainName = dn+","+base;//dn必须带后缀的DC,DC=kittlen,DC=com
        DirContext context = null;
        try {
            context = ldapTemplate.getContextSource().getContext(userDomainName, passWord);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            LdapUtils.closeContext(context);
        }
        return false;
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值