当前位置: 首页>>代码示例>>Java>>正文


Java LdapCtxFactory类代码示例

本文整理汇总了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);
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:10,代码来源:ldapURLContextFactory.java

示例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;
    }

}
 
开发者ID:drinkwater-io,项目名称:drinkwater-java,代码行数:54,代码来源:ActiveDirectoryAuthenticationService.java


注:本文中的com.sun.jndi.ldap.LdapCtxFactory类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。