在使用spring-security-ldap的项目中,我需要执行一些LDAP查询,并添加了spring-data-ldap。突然我不能再连接到嵌入的LDAP注册表,我得到:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'run': Invocation of init method failed; nested exception is org.springframework.ldap.CommunicationException: localhost:8389; nested exception is javax.naming.CommunicationException: localhost:8389 [Root exception is java.net.ConnectException: Connexion refusée (Connection refused)]以下是安全配置,其工作方式与预期一致:
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests().antMatchers("/admins").hasRole("ADMINS")
.antMatchers("/users").hasRole("USERS")
.anyRequest().fullyAuthenticated()
.and()
.httpBasic();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth
.ldapAuthentication()
.userDnPatterns("uid={0},ou=people")
.userSearchBase("ou=people")
.userSearchFilter("uid={0}")
.groupSearchBase("ou=groups")
.groupSearchFilter("uniqueMember={0}")
.contextSource(contextSource())
.passwordCompare()
.passwordAttribute("userPassword");
}
@Bean
public DefaultSpringSecurityContextSource contextSource()
{
log.info("*** SpringSecurityConfig.contextSource(): Inside contextSource");
DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(
Arrays.asList("ldap://localhost:8389/"), "dc=toto,dc=com");
contextSource.afterPropertiesSet();
return contextSource;
}
}现在,如果我想使用spring-data-ldap,我会添加以下内容:
@Repository
public interface MyLdapRepository extends LdapRepository<LdapUser>
{
}
@Entry(base="ou=users", objectClasses = {"person", "inetOrgPerson", "top"})
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class LdapUser
{
@Id
private Name id;
@Attribute(name = "uid")
private String uid;
@Attribute(name = "cn")
private String cn;
}我试着提出一些问题,例如:
@SpringBootApplication
@Slf4j
public class Run extends SpringBootServletInitializer
{
@Autowired
private RdfLdapRepository rdfLdapRespository;
public static void main(String[] args)
{
SpringApplication.run(Run.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder)
{
return builder.sources(Run.class);
}
@PostConstruct
public void setup()
{
log.info("### setup(): the LDIF file has been loaded");
Iterable<LdapUser> users = rdfLdapRespository.findAll();
users.forEach(user -> log.info("\"### setup(): names {}", user.getUid()));
}
}我的连接被拒绝。注释掉setup()方法后,一切又按预期工作了。我怀疑spring-data-ldap使用的Ldaptemplate和安全配置中的DefaultSpringSecurityContextSource之间存在不匹配。
有谁知道这里可能出了什么问题吗?
很多感谢提前;
致以亲切的问候,
尼古拉斯
发布于 2019-08-14 20:46:08
问题解决了。这一切都是因为Spring嵌入式LDAP目录服务器的另一个实例正在同一个Tomcat容器中运行。我不确定这是如何与spring-data-ldap交互的,也不确定为什么它只出现在这个上下文中,但使用以下命令是很重要的,因为它帮助我理解了这个问题:
> lsof -i:8389通过这种方式,我注意到另一个LDAP嵌入式服务器处于活动状态,并且我理解了原因(在同一个Tomcat容器上重复部署的结果)。
https://stackoverflow.com/questions/57484356
复制相似问题