如何将LDAP配置更改为Spring Security实现?

时间:2016-10-31 03:40:01

标签: spring-security ldap

我有这个LDAP配置:

Hashtable env = new Hashtable();

            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, "ldap://.....");
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, "login);
            env.put(Context.SECURITY_CREDENTIALS, password);

            // Create the initial context 
            DirContext ctx = new InitialDirContext(env);
            boolean result = ctx != null;

如何更改Spring Security实现的配置?

auth.ldapAuthentication().....

1 个答案:

答案 0 :(得分:0)

您可以将此示例代码用作模板。就我而言,LDAP提供程序URL为ldap://localhost:1389/dc=example,dc=com,用户条目具有以下DN模式:uid={0},ou=people,dc=example,dc=com。这意味着uid属性将用作用户名。

LdapContextSource ctxSrc = new LdapContextSource();
ctxSrc.setUrl("ldap://localhost:1389");
ctxSrc.setBase("dc=example,dc=com");
ctxSrc.setUserDn("login");
ctxSrc.setPassword("password");
ctxSrc.afterPropertiesSet();

auth
  .ldapAuthentication()
  .contextSource(ctxSrc)
  .userSearchBase("ou=people")
  .userSearchFilter("(uid={0})");
相关问题