用于LDAP的Spring Security Java配置

Ste*_*lon 4 spring ldap spring-security

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

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

DB5*_*DB5 9

你只需使用的url()方法LdapAuthenticationProviderConfigurer.ContextSourceBuilder

所以你可以简单地扩展代码,如下所示:

@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");
}
Run Code Online (Sandbox Code Playgroud)

  • 给出使用.ldif()的例子对任何人来说都是无用的.每个教程都依赖于它,并没有解决任何人的问题. (7认同)