该指南的完整版对我来说甚至无法开箱即用。经过相当多的实验,最终结果如下:
(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”等登录。