【发布时间】:2016-08-12 11:25:22
【问题描述】:
我正在尝试 SpringBoot here 中的 LDAP 身份验证示例
它使用了我认为不适用于我的要求的 ldif 方法,因为我们的 ldap 管理员不会告诉我在哪里可以找到我需要的 ldif。 在 springboot 之前,我曾经使用自己的 ldap 实现而不使用 ldif。有没有办法验证不使用 ldif 只是 SECURITY_AUTHENTICATION.simple ? 下面是我如何在基本 Java 没有 spring 中进行 ldap 安全性。我如何在春天做到这一点,而不使用 ldif 只是基本的用户名密码。
boolean isLdapRegistred(String username, String password) {
boolean result = false;
try {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://10.x.x.x:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "OUR-DOMAIN\\" + username);
env.put(Context.SECURITY_CREDENTIALS, password);
// Create the initial context
DirContext ctx = new InitialDirContext(env);
result = ctx != null;
if (ctx != null)
ctx.close();
System.out.println(result);
return result;
} catch (Exception e) {
System.out.println("oops");
return result;
}
}
以下是 SpringBoots 示例需要使用我的凭据而不是 ldif。
@Configuration
protected static class AuthenticationConfiguration extends
GlobalAuthenticationConfigurerAdapter {
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth
.ldapAuthentication()
.userDnPatterns("uid={0},ou=people")
.groupSearchBase("ou=groups")
.contextSource().ldif("classpath:test-server.ldif");
}
}
【问题讨论】:
-
你试过
.contextSource().url("ldap://10.x.x.x").port("389")而不是.contextSource().ldif("classpath:test-server.ldif");吗? -
仅供参考,@jny,
port()方法的输入参数是 int,而不是 String。
标签: java spring spring-security spring-boot