Java 为什么ldaptemplate(ldapcontextsource)使用基本URL而不是为连接提供的URL?
我正在测试SpringLDAP的故障切换功能 我对连接的配置如下:URL和基本DNJava 为什么ldaptemplate(ldapcontextsource)使用基本URL而不是为连接提供的URL?,java,spring,ldap,spring-ldap,Java,Spring,Ldap,Spring Ldap,我正在测试SpringLDAP的故障切换功能 我对连接的配置如下:URL和基本DN ldap: personDaoImpl: ldapConfigProperties: base: DC=exm,DC=example,DC=com ssl: ldaps://192.168.178.57:636 ldaps://192.168.178.50:636 我正在创建ldap模板,如下所示 @Bean(name = "contextSource&qu
ldap:
personDaoImpl:
ldapConfigProperties:
base: DC=exm,DC=example,DC=com
ssl: ldaps://192.168.178.57:636 ldaps://192.168.178.50:636
我正在创建ldap模板,如下所示
@Bean(name = "contextSource")
public LdapContextSource contextSource() {
LdapContextSource contextSource = new LdapContextSource();
try {
String url = null;
Map<String, Object> baseEnvironmentProperties = new HashMap<String, Object>();
baseEnvironmentProperties.put("com.sun.jndi.ldap.connect.timeout", personDaoImpl.getLdapConfigProperties().getConnectTimeout());
baseEnvironmentProperties.put("com.sun.jndi.ldap.read.timeout", personDaoImpl.getLdapConfigProperties().getReadTimeout());
if (personDaoImpl.getLdapConfigProperties().getConnectionType().toLowerCase() == "insecure") {
url = personDaoImpl.getLdapConfigProperties().getUrl();
} else {
url = personDaoImpl.getLdapConfigProperties().getSsl();
baseEnvironmentProperties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
baseEnvironmentProperties.put(Context.SECURITY_AUTHENTICATION, "simple");
baseEnvironmentProperties.put("java.naming.ldap.version", "3");
baseEnvironmentProperties.put(Context.REFERRAL, "follow");
baseEnvironmentProperties.put("java.naming.ldap.factory.socket",
"com.rockwell.auth.configuration.CustomSSLSocketFactory");
System.setProperty("com.sun.jndi.ldap.connect.pool", "false");
}
String split[] = url.split("\\ ");
contextSource.setUrls(split);
contextSource.setReferral("follow");
contextSource.setBase(personDaoImpl.getLdapConfigProperties().getBase());
contextSource.setUserDn(personDaoImpl.getLdapConfigProperties().getUser());
contextSource.setPassword(personDaoImpl.getLdapConfigProperties().getPassword());
contextSource.setBaseEnvironmentProperties(baseEnvironmentProperties);
contextSource.afterPropertiesSet();
} catch (Exception e) {
System.err.println((new StringBuilder(" LDAP Context Error ")).append(e.getMessage()).toString());
}
return contextSource;
}
@Bean
public LdapTemplate ldapTemplate() {
LdapTemplate ldapTemplate = new LdapTemplate(contextSource());
try {
ldapTemplate.afterPropertiesSet();
personDaoImpl.setLdapTemplate(ldapTemplate);
} catch (Exception e) {
e.printStackTrace();
}
return ldapTemplate;
}
@Bean(name=“contextSource”)
公共LdapContextSource上下文源(){
LdapContextSource contextSource=新的LdapContextSource();
试一试{
字符串url=null;
Map baseEnvironmentProperties=new HashMap();
baseEnvironmentProperties.put(“com.sun.jndi.ldap.connect.timeout”,persondaimpl.getldapconfig属性().getConnectTimeout());
baseEnvironmentProperties.put(“com.sun.jndi.ldap.read.timeout”,persondaimpl.getldapconfig属性().getReadTimeout());
if(persondaImpl.getLdapConfigProperties().getConnectionType().toLowerCase()=“不安全”){
url=personDaoImpl.getldapconfig属性().getUrl();
}否则{
url=personDaoImpl.getldapconfig属性().getSsl();
baseEnvironmentProperties.put(Context.INITIAL_Context_工厂,“com.sun.jndi.ldap.LdapCtxFactory”);
baseEnvironmentProperties.put(Context.SECURITY_身份验证,“simple”);
baseEnvironmentProperties.put(“java.naming.ldap.version”,“3”);
baseEnvironmentProperties.put(Context.reference,“follow”);
baseEnvironmentProperties.put(“java.naming.ldap.factory.socket”,
“com.rockwell.auth.configuration.CustomSSLSocketFactory”);
setProperty(“com.sun.jndi.ldap.connect.pool”、“false”);
}
字符串拆分[]=url.split(\\”;
setURL(拆分);
contextSource.setReferral(“follow”);
setBase(persondaimpl.getldapconfig属性().getBase());
setUserDn(persondaImpl.getLdapConfigProperties().getUser());
setPassword(persondaimpl.getldapconfig属性().getPassword());
setBaseEnvironmentProperties(baseEnvironmentProperties);
contextSource.AfterPropertieSet();
}捕获(例外e){
System.err.println((新的StringBuilder(“LDAP上下文错误”)).append(例如getMessage()).toString());
}
返回contextSource;
}
@豆子
公共LdapTemplate LdapTemplate(){
LdapTemplate LdapTemplate=新的LdapTemplate(contextSource());
试一试{
ldapTemplate.afterPropertieSet();
persondaimpl.setLdapTemplate(ldapTemplate);
}捕获(例外e){
e、 printStackTrace();
}
返回ldapTemplate;
}
我使用ldapTemplate查询用户,如下所示:
public User getUserByLogonName(String logon) {
User user = null;
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectclass", "User")).and(new EqualsFilter("sAMAccountName", logon));
List<User> userList = ldapTemplate.search("", filter.toString(), getContextMapper(""));
if(userList != null && userList.size() > 0){
user = userList.get(0);
}else{
user = new User();
}
return user;
}
公共用户getUserByLogonName(字符串登录){
User=null;
AndFilter filter=新AndFilter();
和(新的EqualFilter(“对象类”、“用户”))和(新的EqualFilter(“sAMAccountName”,登录));
List userList=ldapTemplate.search(“”),filter.toString(),getContextMapper(“”);
if(userList!=null&&userList.size()>0){
user=userList.get(0);
}否则{
user=新用户();
}
返回用户;
}
当我尝试搜索用户时,我发现ldapTemplate正在使用基本DN进行连接,而不是提供的URL:
org.springframework.ldap.PartialResultException: nested exception is javax.naming.PartialResultException [Root exception is javax.naming.CommunicationException: exm.example.com:636 [Root exception is java.net.UnknownHostException: exm.example.com]]
at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:216)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:385)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:328)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:629)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:570)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:530)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:546)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:562)
at com.rockwell.ldap.ad.domain.Impl.UserRepoImpl.getUserByLogonName(UserRepoImpl.java:232)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:503)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:478)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy78.getUserByLogonName(Unknown Source)
at com.rockwell.ldap.ad.services.UserService.createUser(UserService.java:195)
at com.rockwell.auth.controller.UserController.lambda$createUser$2(UserController.java:114)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.naming.PartialResultException [Root exception is javax.naming.CommunicationException: exm.example.com:636 [Root exception is java.net.UnknownHostException: exm.example.com]]
at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.hasMoreImpl(AbstractLdapNamingEnumeration.java:237)
at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.hasMore(AbstractLdapNamingEnumeration.java:189)
at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:365)
... 28 more
Caused by: javax.naming.CommunicationException: exm.example.com:636 [Root exception is java.net.UnknownHostException: exm.example.com]
at com.sun.jndi.ldap.LdapReferralContext.<init>(LdapReferralContext.java:96)
at com.sun.jndi.ldap.LdapReferralException.getReferralContext(LdapReferralException.java:150)
at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.hasMoreReferrals(AbstractLdapNamingEnumeration.java:325)
at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.hasMoreImpl(AbstractLdapNamingEnumeration.java:227)
... 30 more
Caused by: java.net.UnknownHostException: exm.example.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
at sun.reflect.GeneratedMethodAccessor39.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.jndi.ldap.Connection.createSocket(Connection.java:311)
at com.sun.jndi.ldap.Connection.<init>(Connection.java:203)
at com.sun.jndi.ldap.LdapClient.<init>(LdapClient.java:137)
at com.sun.jndi.ldap.LdapClient.getInstance(LdapClient.java:1614)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2746)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:319)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:192)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:151)
at com.sun.jndi.url.ldap.ldapURLContextFactory.getObjectInstance(ldapURLContextFactory.java:52)
at javax.naming.spi.NamingManager.getURLObject(NamingManager.java:601)
at javax.naming.spi.NamingManager.processURL(NamingManager.java:381)
at javax.naming.spi.NamingManager.processURLAddrs(NamingManager.java:361)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:333)
at com.sun.jndi.ldap.LdapReferralContext.<init>(LdapReferralContext.java:119)
... 33 more
07:52:38.778 [http-nio-8090-exec-8] WARN com.rockwell.auth.util.CustomExceptionHandler - org.springframework.ldap.PartialResultException
org.springframework.ldap.PartialResultException:嵌套异常为javax.naming.PartialResultException[根异常为javax.naming.CommunicationException:exm.example.com:636[根异常为java.net.UnknownHostException:exm.example.com]]
位于org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:216)
位于org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:385)
位于org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:328)
位于org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:629)
位于org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:570)
位于org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:530)
位于org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:546)
位于org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:562)
位于com.rockwell.ldap.ad.domain.Impl.UserRepoImpl.getUserByLogonName(UserRepoImpl.java:232)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:498)
位于org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:503)
位于org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:478)
位于org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
在org.springframework.aop.framework.ReflectiveMethodInvocation.procedue(ReflectiveMethodInvocation.java:179)上
位于org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
在org.springframework.aop.framework.ReflectiveMethodInvocation.procedue(ReflectiveMethodInvocation.java:179)上
位于org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
在org.springframework.aop.framework.ReflectiveMethodInvocation.procedue(ReflectiveMethodInvocation.java:179)上
位于org.springframework.aop.framewor