本文整理汇总了Java中com.sun.jndi.ldap.LdapCtxFactory类的典型用法代码示例。如果您正苦于以下问题:Java LdapCtxFactory类的具体用法?Java LdapCtxFactory怎么用?Java LdapCtxFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
LdapCtxFactory类属于com.sun.jndi.ldap包,在下文中一共展示了LdapCtxFactory类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getObjectInstance
import com.sun.jndi.ldap.LdapCtxFactory; //导入依赖的package包/类
public Object getObjectInstance(Object urlInfo, Name name, Context nameCtx,
Hashtable<?,?> env) throws Exception {
if (urlInfo == null) {
return new ldapURLContext(env);
} else {
return LdapCtxFactory.getLdapCtxInstance(urlInfo, env);
}
}
示例2: authenticate
import com.sun.jndi.ldap.LdapCtxFactory; //导入依赖的package包/类
@Override
public Map<String, String> authenticate(Credentials credentials) throws Exception {
Hashtable props = new Hashtable();
String principalName = credentials.getUser() + "@" + domainName;
props.put(Context.SECURITY_PRINCIPAL, principalName);
props.put(Context.SECURITY_CREDENTIALS, credentials.getPassword());
DirContext context;
try {
//context = LdapCtxFactory.getLdapCtxInstance("ldap://" + serverName + "." + domainName + '/', props);
context = LdapCtxFactory.getLdapCtxInstance("ldap://" + serverName + '/', props);
System.out.println("Authentication succeeded!");
// locate this user's record
SearchControls controls = new SearchControls();
controls.setSearchScope(SUBTREE_SCOPE);
NamingEnumeration<SearchResult> renum = context.search(toDC(domainName),
"(& (userPrincipalName=" + principalName + ")(objectClass=user))", controls);
if (!renum.hasMore()) {
throw new Exception("Cannot locate user information for " + credentials.getUser());
}
SearchResult result = renum.next();
List<String> groups = new ArrayList<String>();
Attribute memberOf = result.getAttributes().get("memberOf");
if (memberOf != null) {// null if this user belongs to no group at all
for (int i = 0; i < memberOf.size(); i++) {
Attributes atts = context.getAttributes(memberOf.get(i).toString(), new String[]{"CN"});
Attribute att = atts.get("CN");
groups.add(att.get().toString());
}
}
context.close();
String roles = groups.stream().collect(Collectors.joining(","));
Map<String, String> claims = new HashMap<>();
claims.put("sub", credentials.getUser());
claims.put("name", credentials.getUser());
claims.put("roles", roles);
return claims;
} catch (Exception a) {
logger.error("authentication exception " + a.getMessage());
return null;
}
}