【问题标题】:Spring Security + Auth LDAP : BindRequest and UnbindRequest?Spring Security + Auth LDAP:BindRequest 和 UnbindRequest?
【发布时间】:2018-04-15 22:14:42
【问题描述】:

经过几天的 Google 研究、阅读 F* Spring 安全手册和测试,我变得绝望了……

上下文:我正在使用 Eureka 等实现微服务架构...... 我实现了一个身份验证服务,它与 MySQL 身份验证数据库配合得很好。但是现在,我想通过一个可以正常工作的 OpenLDAP 加入我公司的 LDAP。

所以,我正在尝试使用我的 spring 安全身份验证加入 LDAP。

我的configure()方法的代码(我换了我的公司和域名,账号{0}是“test”):

auth.ldapAuthentication()
        .contextSource()
        .url("ldap://myldap/ou=users,dc=mydomain,dc=mycompany")
    .and()
        .userDnPatterns("cn={0}");

我也尝试了不同的方法来编写这个,但我一直收到 Bad Credentials 或 LDAP 32 错误。使用 userDnPattern、usersearchbase 方法、passwordcompare、passwordencoder 等。我还尝试将 DC 放在 root() 方法中,将 OU 放在 group...() 方法中,没有任何变化(我认为实际上 Spring Security 在发送 LDAP 请求之前对这些参数进行了智能排序)。老实说,我尝试了 357654 种不同的方式来编写 configure() 方法......

问题在于:当我将相同的配置、凭据、域...放入 LDAP 资源管理器软件时,它可以正常工作。

所以我监控了与 Wireshark 的 LDAP 网络交换,我看到了这个: Wireshark screen

我们可以看到,交换了 8 个请求。前5个还可以。它正确地找到了我的帐户“测试”。但是有三个 over requests(其中 unbindRequest 会返回)。

问题是 Spring 给了我最后一个请求的结果,并说我的帐户不存在或凭据不起作用等...

你有这方面的线索吗?你知道 Spring Security 是如何质疑 LDAP 的吗?如何才能将我的 LDAP 与框架充分联系起来?

感谢您的阅读。

帮助我 Stack Overflow,你是我唯一的希望......

【问题讨论】:

    标签: spring authentication spring-boot spring-security ldap


    【解决方案1】:

    我终于找到了问题并得到了解决方案。

    我的企业 LDAP 是 AD 之上的 LDAP。

    而且这个 LDAP+AD 需要绑定认证,并且不授权匿名绑定然后认证。

    在 Spring Security 中,有一个对象可以做到这一点:BindAuthenticator

    这就是我尝试使用它的方式(并且有效)。

     @Override
     protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
          authManagerBuilder.authenticationProvider(ldapAuthenticationProvider()).userDetailsService(userDetailsService());
     }
    
     @Bean
     public LdapAuthenticationProvider ldapAuthenticationProvider() throws Exception {
          LdapAuthenticationProvider lAP = new LdapAuthenticationProvider(ldapAuthenticator(), ldapAuthoritiesPopulator());
          return lAP;
     }
    
     @Bean
     public LdapContextSource ldapContextSource() throws Exception {
          DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource([URL of the LDAP]);
    
          return contextSource;
     }
    
     @Bean
     public LdapAuthenticator ldapAuthenticator() throws Exception {
          BindAuthenticator authenticator = new BindAuthenticator(ldapContextSource());
          authenticator.setUserDnPatterns(new String[] {"CN={0},"+[MY ENTERPRISE LDAP FILTER]});          
          return authenticator;
     }
    

    希望这个示例代码可以帮助一些人...

    谢谢!

    【讨论】:

      猜你喜欢
      • 2016-09-10
      • 2016-10-02
      • 2017-08-13
      • 2020-06-21
      • 2013-07-13
      • 2015-12-02
      • 2013-07-20
      • 2017-07-08
      相关资源
      最近更新 更多