ldap

private static ResourceBundle bundle = ResourceBundle.getBundle("/properties/connection");

private static String url = bundle.getString("ldapUrl");

private static String principal = bundle.getString("ldapUser");

private static String password = bundle.getString("ldapPwd");

private static String baseDir = bundle.getString("ldapBaseDir");

@Autowired

private Log_ log;

public boolean  checkADLDSLdapUserInfo(String userId,String password) throws NamingException {

    boolean falg=false;

    Hashtable<String,String> hash = new Hashtable<String,String>();

     hash.put("java.naming.factory.initial","com.sun.jndi.ldap.LdapCtxFactory");

     hash.put(Context.PROVIDER_URL, url);

     hash.put(Context.SECURITY_AUTHENTICATION, "simple");

     hash.put(Context.REFERRAL, "follow");

     hash.put(Context.SECURITY_PRINCIPAL, userId);

     hash.put(Context.SECURITY_CREDENTIALS, password);

 

    SearchControls ctls = new SearchControls();

    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    LdapContext ldapContext=null;

    try {

    ldapContext = new InitialLdapContext(hash, null);

    falg=true;

    } catch (NamingException e) {

    log.service().error("User authentication failure");

log.service().error("ldap namingException message -----"+e.getMessage());

    throw e;

    }finally{

    colseLdapConn(ldapContext);

    }

    return falg;

    }

private void colseLdapConn(LdapContext ldapContext) {

try {

if(null!=ldapContext){

ldapContext.close();

}

} catch (NamingException e) {

log.service().info("ldap close exception message -----"+e.toString());

}

}

public String getUserName(String userId) throws NamingException{

InitialDirContext ctx = null;

try{

Hashtable<String,String> hash = new Hashtable<String,String>();

      hash.put("java.naming.factory.initial","com.sun.jndi.ldap.LdapCtxFactory");

      hash.put(Context.PROVIDER_URL, "ldap://ip:port");

      hash.put(Context.SECURITY_AUTHENTICATION, "simple");

      hash.put(Context.SECURITY_PRINCIPAL, "xxx  user");

      hash.put(Context.SECURITY_CREDENTIALS, "password");

      hash.put(Context.REFERRAL, "follow");

      ctx = new InitialLdapContext(hash, null);

         

      String[] userAttrs = {

        "sn", "sAMAccountName","displayName", "givenName",

        "mail", "distinguishedName",

        "memberOf","department","title","telephoneNumber","company"};

     

      SearchControls ctls = new SearchControls();

      ctls.setReturningAttributes(userAttrs);

      ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);

      String baseDir = "OU=XX,DC=XX,DC=com";

      SearchResult result = (SearchResult) ctx.search(baseDir,"sAMAccountName=" + userId, ctls).next();

      Attributes attrs = result.getAttributes();

      return attrs.get("displayName").get(0).toString();

}

catch (NamingException e) {

log.service().error("ldap namingException message -----"+e.getMessage());

}

finally{

if(ctx!=null){

ctx.close();

}

}

return StringUtils.EMPTY;

}

public List<String> getEmpGroups(String userId) throws NamingException {

InitialDirContext ctx  = null;

try {

Hashtable<String, String> hash = new Hashtable<String, String>();

hash.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");

hash.put(Context.SECURITY_AUTHENTICATION, "simple");

hash.put(Context.PROVIDER_URL, url);

hash.put(Context.SECURITY_PRINCIPAL, principal);

hash.put(Context.SECURITY_CREDENTIALS, password);

hash.put(Context.REFERRAL, "follow");

ctx = new javax.naming.ldap.InitialLdapContext(hash, null);

String[] userAttrs = { "memberOf" };

SearchControls ctls = new SearchControls();

ctls.setReturningAttributes(userAttrs);

ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);

SearchResult result = (SearchResult) ctx.search(baseDir, "cn=" + userId, ctls).next();

Attribute attr = result.getAttributes().get("memberOf");

List<String> groupList = new ArrayList<String>();

for (int i = 0; i < attr.size(); i++) {

groupList.add(attr.get(i).toString().split(",")[0].split("=")[1]);

}

return groupList;

catch (Exception e) {

throw e;

}finally{

if(ctx!=null){

ctx.close();

}

}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值