【问题标题】:Issue with Spring Boot Guide - Authenticating a User with LDAPSpring Boot 指南的问题 - 使用 LDAP 验证用户
【发布时间】:2020-02-02 23:24:13
【问题描述】:

Spring Boot 入门指南“使用 LDAP 验证用户”给出 java.net.ConnectException: Connection denied

我刚刚按照此入门指南中的分步说明进行操作 -

https://spring.io/guides/gs/authenticating-ldap/

在 STS 中做过开发

与上面 spring.io 的示例相同

在最后,指南指出用户名= bob 和密码= bobspassword 应该有一个干净的登录名

当我在登录表单中输入相同的凭据时,我的另一个应用程序会生成此错误-

本地主机:8389;嵌套异常是 javax.naming.CommunicationException: localhost:8389 [根异常是 java.net.ConnectException: Connection denied (Connection denied)]

【问题讨论】:

  • “连接被拒绝”意味着没有服务在“localhost:8389”上侦听您的 ldap 服务器是否在 localhost:8389 上运行? (这有点不寻常)显示代码和日志或结果并阅读:stackoverflow.com/help/how-to-ask

标签: spring-boot ldap


【解决方案1】:

我也遇到了同样的问题,发现我在build.gradle文件中配置了一些依赖错误。

这是我的依赖项(修复后):

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.ldap:spring-ldap-core' // Essential
    implementation 'org.springframework.security:spring-security-ldap'
    implementation 'com.unboundid:unboundid-ldapsdk' // (not using testImplementation)
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'org.springframework.security:spring-security-test'
}

这是我从 javabrains.io 教程中学到的我自己的存储库的链接。 https://github.com/tkhenghong/spring-security-ldap

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    问题是https://spring.io/guides/gs/authenticating-ldap/ 的指南没有提到如何设置您的 application.properties 文件。

    解决方案:您需要在 resources/application.properties 文件中设置以下属性

    spring.ldap.embedded.port=8389
    spring.ldap.embedded.ldif=classpath:test-server.ldif
    spring.ldap.embedded.base-dn=dc=springframework,dc=org
    

    将上述代码复制到您的 application.properties 文件中,重新启动您的 Spring 应用程序,它应该可以工作了。

    感谢 tkhenghong 的回答和他上传到他的 github 的代码,我发现了这一点。

    【讨论】:

      【解决方案3】:

      该指南的完整版对我来说甚至无法开箱即用。经过相当多的实验,最终结果如下:

      (1) 应用程序.properties

      spring.ldap.embedded.ldif=classpath:test-server.ldif
      spring.ldap.embedded.base-dn=dc=springframework,dc=org
      spring.ldap.embedded.port=8399 
      

      注意这里是 8399,而不是 8389。8389 正在我的 Windows 10 上监听,我通过执行 netstat -an |find /i "389" 验证了这一点。但即使这样工作,Spring Security 登录页面仍然抱怨嵌入式 ldap 连接拒绝端口 8399。这就是激励我将端口号从 8389 更改为 8399 的原因。请注意,我首先在 Windows 防火墙中添加了一个新的“入站规则”用于 8399。点击此链接获取有关如何打开或关闭端口 https://docs.bitnami.com/installer/faq/windows-faq/administration/use-firewall-windows/ 的说明

      (2) 根据 Spring Guides 提供的示例代码,更改/删除以下两行注释:

        @Override
        public void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth
            .ldapAuthentication()
              .userDnPatterns("uid={0},ou=people")
              .groupSearchBase("ou=groups")
              .contextSource()
                .url("ldap://localhost:8389/dc=springframework,dc=org")//CHANGE 8389 to 8399
                .and()
              .passwordCompare()
                .passwordEncoder(new BCryptPasswordEncoder()) //REMOVE this line
                .passwordAttribute("userPassword");
        }
      }
      

      如果您刚刚开始学习使用 LDAP 进行身份验证的 Spring Guides 教程,那么删除 .passwordEncoder(new BCryptPasswordEncoder()) 会减少复杂性。如果你不喜欢偷工减料,你需要参考其他一些很棒的 Stackoverflow 帖子,了解如何使 passwordEncoder 工作。现在,我的解决方案仅适用于进行最简单的测试,例如用于 uid 的“bob”和用于 userPassword 的“bobspassword”。如上所示,如果不删除 BCrytPasswordEncoder(),您将在使用“bob”和“bobspassword”进行测试时看到警告:“Encoded passoword doesn't look like BCrypt”。

      这就是我偏离指南的全部内容,然后我可以使用 test-server.ldif 中预定义的“bob”和“bobspassword”等登录。

      【讨论】:

      • 这确实对我有用...我想 OP 应该考虑接受答案
      猜你喜欢
      • 2022-01-20
      • 1970-01-01
      • 2020-05-22
      • 2012-09-04
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 2015-11-22
      相关资源
      最近更新 更多