【问题标题】:Spring LdapRepository returns no results while Spring Security works在 Spring Security 工作时,Spring LdapRepository 不返回任何结果
【发布时间】: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


    【解决方案1】:

    我找到了解决办法。

    Spring 似乎假设UserobjectClassUser,如果它没有明确设置的话。设置正确的 objectClasses 可以解决此问题。

    【讨论】:

      猜你喜欢
      • 2021-11-23
      • 2017-08-29
      • 2015-11-02
      • 2018-10-02
      • 1970-01-01
      • 2018-07-13
      • 2017-04-14
      • 2021-07-28
      • 1970-01-01
      相关资源
      最近更新 更多