【发布时间】:2018-01-22 11:34:41
【问题描述】:
我的问题
我正在使用 LdapRepository 从 Ldap 服务器获取用户信息。这个 Ldap 服务器也被 Spring Security 查询以验证用户。
我的问题是,Spring Security 能够找到并识别用户,而我无法使用相同的 LdapContextSource 通过我的 LdapRepository 找到用户。以任何方式查询 LdapRepository 都不会返回结果(null 或空列表)。
我的尝试
- 直接使用ldapsearch 工具 - 有效
- 使用
LdapQuery代替findByUsername方法 - 不起作用 -
findAll()(CrudRepository) 等测试方法 - 返回一个空列表 - 试图从
spring-ldap-Seems to be impossible?获取日志
使用的 ldapsearch 命令:ldapsearch -x -H ldaps://<domain> -b o=<org> uid=<uid>
在 Wireshark 中查看流量(使用 ldap 而不是 ldaps)看起来 LdapRepository 根本没有执行任何查询,连接只是打开和关闭,结果为 0。
相关代码
LdapContextSource的配置
@Bean
public LdapContextSource contextSource() {
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl("ldaps://<domain>");
contextSource.setBase("o=<org>");
return contextSource;
}
安全配置
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private final AuthenticationManagerBuilder authenticationManagerBuilder;
private final LdapContextSource ldapContext;
public SecurityConfiguration(AuthenticationManagerBuilder authenticationManagerBuilder, LdapContextSource ldapContext) {
this.authenticationManagerBuilder = authenticationManagerBuilder;
this.ldapContext = ldapContext;
}
@PostConstruct
public void init() {
try {
authenticationManagerBuilder
.ldapAuthentication()
.contextSource(ldapContext)
.userSearchFilter("(uid={0})");
} catch (Exception e) {
throw new BeanInitializationException("Security configuration failed", e);
}
}
}
用户存储库
@Repository
public interface UserRepository extends LdapRepository<User> {
User findByUsername(String username);
List<User> findByUsernameLikeIgnoreCase(String username);
}
用户
@Entry(
objectClasses = {})
public class User {
@Id
private Name id;
@Attribute(name = "uid", readonly = true)
private String username;
public Name getId() {
return id;
}
public String getUsername() {
return username;
}
}
【问题讨论】:
标签: java spring spring-data spring-ldap spring-repositories