用于LDAP的Spring Security Java配置

时间:2014-11-03 18:31:47

标签: spring spring-security ldap

如何设置spring安全性LDAP配置的URL? 有很多基于xml的示例,但我找不到一个java配置示例来复制下面的xml行。我假设它是在弹簧指南中使用嵌入式ldap的下面的java代码块中配置的,但我们如何设置外部URL?

<ldap-server id="ldapServer" url="ldap://example.com:PORT/dc=example,dc=com" />
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
    auth.ldapAuthentication()
            .userDnPatterns("uid={0},ou=people")
            .groupSearchBase("ou=groups")
            .contextSource()
                .ldif("classpath:test-server.ldif");
}

1 个答案:

答案 0 :(得分:9)

您只需使用LdapAuthenticationProviderConfigurer.ContextSourceBuilder

url()方法即可

因此,您可以按如下方式简单扩展代码:

@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
    auth.ldapAuthentication()
            .userDnPatterns("uid={0},ou=people")
            .groupSearchBase("ou=groups")
            .contextSource()
                .ldif("classpath:test-server.ldif")
                .url("ldap://example.com:PORT/dc=example,dc=com");
}