由于硬件缺乏,这几天在折腾环境,然后莫名其妙的在之前搭建好的域控制器上提示IP冲突,之后springLdap和AD之间使用Ldaps通信时就开始报错。来来回回折腾了不少时间,过程中发现之前配置的方法都忘记了,网上也没有一个比较详细的教程,所以在这里写下来记录一下。
问题:在使用Ldaps通信时程序报错
Path does not chain with any of the trust anchors
网上找到的关于这个异常的信息是由于java7中存在的一处bug,但是我的环境是java8,所以肯定不会是这个问题。开始怀疑是证书的问题,重新导入了证书问题依旧。最后读取了快照,重新建域,问题解决。
1使用SpringLdap,在pom文件中导入相关信息,这里注意下SpringLdap最新版本对Spring版本要求较高,我使用SpringLdap2.3.1时Spring的版本为4.3.6。如果使用SpringBoot搭建环境,请使用SpringBoot1.5.1以上的版本。
2在Spring主配置类中注册SpringLdap的Bean。使用时注入ldapTemplate就可以了。
@Bean
public LdapTemplate ldapTemplate() {
return new LdapTemplate(contextSourceTarget());
}
@Bean
public LdapContextSource contextSourceTarget() {
LdapContextSource ldapContextSource = new LdapContextSource();
ldapContextSource.setUrl(ldapUrl);
ldapContextSource.setBase(ldapBase);
ldapContextSource.setUserDn(ldapUserDn);
ldapContextSource.setPassword(ldapUserPwd);
ldapContextSource.setReferral(ldapReferral);
return ldapContextSource;
}
3认证,可以参考SpringLdap的说明文档,使用域账户的userDN和password进行认证。
ctx = ldapTemplate.getContextSource().getContext(userDn, password);
注意这里的userDN是用户名+域名的形式,即username@xxx.xxx的形式。不要忘记使用fianlly来关闭ctx的连接。
LdapUtils.closeContext(ctx);
4在域控制器上安装证书服务,一路默认就好。安装好之后通过http://证书服务器IP地址/certsrv访问证书发布页面。选择下载证书,在跳转到的页面中下载证书。
