【发布时间】:2018-04-15 02:05:24
【问题描述】:
我对 LDAP 几乎一无所知,对 spring 安全性更是一无所知,但我正在尝试配置一个 spring boot 应用程序来针对 ldap 实例进行身份验证,但我被卡住了。
我在 adldap.company.com 获得了 ldap 服务器名称和 dc=ad,dc=company,dc=com 的基本 dn
我有一些 python 代码可以进行简单的绑定并且可以工作。
LDAP_USERNAME = 'username@ad.company.com'
LDAP_PASSWORD = 'password'
base_dn = 'dc=ad,dc=company,dc=com' # not used for bind I guess, only search
try:
ldap_client = ldap.initialize('ldap://adldap.company.com')
ldap_client.set_option(ldap.OPT_REFERRALS,0)
ldap_client.simple_bind_s(LDAP_USERNAME, LDAP_PASSWORD)
except ldap.INVALID_CREDENTIALS as e:
ldap_client.unbind()
return 'Wrong username and password: %s' % e
except ldap.SERVER_DOWN:
return 'AD server not available'
如果我运行此代码,它似乎成功绑定为“username@ad.company.com”,密码为“password”。
我还有一个我认为应该处理身份验证的 WebSecurityConfig 类:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/secure")
.authorizeRequests()
.anyRequest().fullyAuthenticated()
.and()
.httpBasic();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.ldapAuthentication()
.userDnPatterns("uid={0}")
.contextSource()
.url("ldap://adldap.company.com");
//.url("ldap://adldap.company.com/dc=ad,dc=company,dc=com");
}
}
当我在应用程序中转到 /secure 时,我会弹出一个基本的身份验证,但是我尝试输入的任何内容都会让我 401 Unauthorized。我试过“username@ad.company.com”,没有域,把这些东西放在 userDnPatterns 中,比如 {0}@adldap.company.com 和一堆其他东西。我尝试过使用不同的 URL,其中包含基本 dn。似乎没有任何效果。我错过了什么?
另外,这是对用户进行身份验证的正确方法吗?我已经阅读了有关绑定身份验证和绑定和搜索的内容,但服务器不允许匿名绑定,所以我想我需要某种可以绑定和执行搜索的“应用程序用户”,对吧?那是“更好”吗?
【问题讨论】:
-
您是否在 org.springframework.security.ldap 上启用了 DEBUG 日志记录?
-
是的,它基本上给了我一个 InvalidCredentials 错误。不过密码没问题,就是觉得格式有问题。
-
@SébastienHelbert 我知道已经有一段时间了,但我在 atm 面临类似的问题。你能看看我的question,看看你是否也遇到过同样的麻烦,能帮我解决吗? :)
标签: java spring spring-boot active-directory ldap