spring LdapTemplate创建AD账户时创建密码

        微软的AD服务器实现了Ldap协议,可以通过Ldap协议来访问AD服务器。AD服务器有两个端口,一个是389,一个是636。端口389是普通端口,可以用来查询,也可以用来创建和更新账户信息,但不能创建和更新密码。AD账户的密码操作需要通过636这个加密端口来执行。

        通过636端口访问AD服务器时需要加密证书,所以要先从AD服务器生成密钥证书。注意需要导出两个证书,分别是域根证书和计算机证书。使用要java工具将证书导入到创建的密钥库中就可以在代码中使用了。

        LdapTemplate配置代码如下:

@Configuration
public class LdapConfig {

    @Value("${keystore_url}")
    private String keystoneURL;

    @Bean
    @ConfigurationProperties("spring.ldap")
    public LdapProperties ldapProperties() {
        return new LdapProperties();
    }

    @Bean
    @Primary
    public LdapTemplate getLdapTemplate() {
        System.setProperty("com.sun.jndi.ldap.object.disableEndpointIdentification", "true");
        System.setProperty("javax.net.ssl.trustStore", keystoneURL);
        System.setProperty("javax.net.ssl.trustStorePassword", "12345.com");

        LdapProperties properties = ldapProperties();
        // 普通ldap连接使用普通的Context配置
        LdapContextSource contextSource = new SSLLdapContextSource();
        String url = properties.getUrls()[0];

        contextSource.setUserDn(properties.getUsername());
        contextSource.setPassword(properties.getPassword());
        contextSource.setUrl(url);
        contextSource.setBase(properties.getBase());
        contextSource.setAnonymousReadOnly(false);
        contextSource.setPooled(false);
        contextSource.afterPropertiesSet();

        LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
        ldapTemplate.setIgnorePartialResultException(true);
        return ldapTemplate;
    }
}

期中keystore_url是密钥库所在目录。

在创建AD账户时的注意事项:

1、BasicAttribute ocAttr = new BasicAttribute("objectclass");
            ocAttr.add("top");
            ocAttr.add("person");
            ocAttr.add("organizationalPerson");
            ocAttr.add("user");// 上面几个objectclass是固定的,如果层级更深的话不知道是什么状况
            Attributes attrs = new BasicAttributes(true);
            attrs.put(ocAttr);// 必须有baseattribute

这段代码基本是固定的

2、注意空值

3、attrs.put("pwdLastSet", "0"); 设置首次登录修改密码

4、String newQuotedPassword = "\"" + user.getPassword() + "\"";
      byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");
      attrs.put("unicodePwd", newUnicodePassword);

      以上代码创建密码

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值