此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring LDAP 3.2.8! |
使用 Spring LDAP 进行用户认证
本节介绍使用 Spring LDAP 进行用户身份验证。它包含以下主题:
基本身份验证
虽然ContextSource
就是提供DirContext
实例供使用LdapClient
和LdapTemplate
,您还可以使用它来根据 LDAP 服务器对用户进行身份验证。这getContext(principal, credentials)
method 的ContextSource
确实如此。它构造了一个DirContext
实例根据ContextSource
配置,并使用提供的主体和凭证对上下文进行身份验证。自定义身份验证方法可能类似于以下示例:
public boolean authenticate(String userDn, String credentials) {
DirContext ctx = null;
try {
ctx = contextSource.getContext(userDn, credentials);
return true;
} catch (Exception e) {
// Context creation failed - authentication did not succeed
logger.error("Login failed", e);
return false;
} finally {
// It is imperative that the created DirContext instance is always closed
LdapUtils.closeContext(ctx);
}
}
这userDn
提供给authenticate
method 需要是要进行身份验证的用户的完整 DN(无论base
设置ContextSource
).通常需要根据用户名(例如)执行 LDAP 搜索才能获取此 DN。以下示例显示了如何执行此作:
private String getDnForUser(String uid) {
List<String> result = ldapClient.search()
.query(query().where("uid").is(uid))
.toList((Object ctx) -> ((DirContextOperations) ctx).getNameInNamespace());
if(result.size() != 1) {
throw new RuntimeException("User not found or not unique");
}
return result.get(0);
}
这种方法有一些缺点。您被迫关注用户的 DN,您只能搜索用户的 uid,并且搜索始终从树的根(空路径)开始。更灵活的方法允许您指定搜索库、搜索筛选条件和凭据。Spring LDAP 在LdapClient
,它提供此功能。
当您使用此方法时,身份验证将变得如此简单,如下所示:
ldapClient.authenticate().query(query().where("uid").is("john.doe")).password("secret").execute();
如在已验证的上下文中执行作中所述,某些设置可能需要您执行其他作才能进行实际验证。有关详细信息,请参阅对 Authenticated Context 执行作。 |
不要编写自己的自定义身份验证方法。使用 Spring LDAP 中提供的那些。 |
对已验证的上下文执行作
某些身份验证方案和 LDAP 服务器需要对创建的DirContext
实例进行实际身份验证。您应该测试并确保服务器设置和身份验证方案的行为方式。如果不这样做,可能会导致用户被允许进入您的系统,而不管提供的 DN 和凭证如何。以下示例显示了 authenticate 方法的朴素实现,其中硬编码的lookup
operation 在经过身份验证的上下文中执行:
public boolean myAuthenticate(String userDn, String credentials) {
DirContext ctx = null;
try {
ctx = contextSource.getContext(userDn, credentials);
// Take care here - if a base was specified on the ContextSource
// that needs to be removed from the user DN for the lookup to succeed.
ctx.lookup(userDn);
return true;
} catch (Exception e) {
// Context creation failed - authentication did not succeed
logger.error("Login failed", e);
return false;
} finally {
// It is imperative that the created DirContext instance is always closed
LdapUtils.closeContext(ctx);
}
}
如果该作可以作为回调接口的实现提供,而不是将作限制为始终为lookup
.Spring LDAP 包括AuthenticatedLdapEntryContextMapper
callback 接口和相应的authenticate
方法。
此方法允许在经过身份验证的上下文中执行任何作,如下所示:
AuthenticatedLdapEntryContextMapper<DirContextOperations> mapper = new AuthenticatedLdapEntryContextMapper<DirContextOperations>() {
public DirContextOperations mapWithContext(DirContext ctx, LdapEntryIdentification ldapEntryIdentification) {
try {
return (DirContextOperations) ctx.lookup(ldapEntryIdentification.getRelativeName());
}
catch (NamingException e) {
throw new RuntimeException("Failed to lookup " + ldapEntryIdentification.getRelativeName(), e);
}
}
};
ldapClient.authenticate().query(query().where("uid").is("john.doe")).password("secret").execute(mapper);
使用 Spring Security
虽然前面部分中描述的方法可能足以满足简单的身份验证方案,但此领域的要求通常会迅速扩展。适用许多方面,包括身份验证、授权、Web 集成、用户上下文管理等。如果您怀疑这些要求可能超出简单的身份验证范围,那么您绝对应该考虑使用 Spring Security 来实现您的安全目的。它是一个功能齐全、成熟的安全框架,可解决上述方面以及其他几个方面。