Spring Security + Auth LDAP:BindRequest和UnbindRequest?

时间:2017-11-03 08:31:08

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

经过几天的Google研究,阅读F * Spring安全手册并进行测试,我变得绝望......

背景信息:我正在与Eureka等实施微服务架构...... 我实现了一个Auth服务,它可以很好地与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 ...()方法中,没有变化(我认为实际上Spring Security在发送LDAP请求之前巧妙地对这些参数进行排序)。说实话,我尝试了357654种不同的方法来编写configure()方法......

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

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

我们可以看到,已经交换了8个请求。前5个都可以。它找到了我的帐号" test"正确。但是有三个超过请求(unbindRequest会返回)。

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

你有这方面的线索吗?你知道Spring Security如何解决LDAP的问题吗?如何与框架充分联系我的LDAP?

感谢您的阅读。

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

1 个答案:

答案 0 :(得分:2)

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

我的企业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;
 }

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

谢谢!