java实现ad域登陆验证

这里的username和password都是域登陆账号密码

  public String checkMail(String userName,String password) {
      String host = "192.168.1.2"; // AD服务器IP(访问地址)
      String port = "389"; // 端口
      // String domain = "@domain.com.cn"; //邮箱的后缀名
      String user="trustview\\tengfei";//这里有两种格式,domain\User或邮箱的后缀名,建议用domain\User这种格式
      String url = new String("ldap://" + host + ":" + port);
      // String user = userName.indexOf(domain) > 0 ? userName : userName + domain;
      Hashtable env = new Hashtable();
      DirContext ctx;
      env.put(Context.SECURITY_AUTHENTICATION, "simple");//ldap访问级别
      env.put(Context.SECURITY_PRINCIPAL, user); //ad user
      env.put(Context.SECURITY_CREDENTIALS, password);//ad password
      env.put(Context.INITIAL_CONTEXT_FACTORY,
      "com.sun.jndi.ldap.LdapCtxFactory");//ldap工厂类
      env.put(Context.PROVIDER_URL, url);
      try {
      ctx = new InitialDirContext(env);
      ctx.close();
      return userName; //验证成功返回name
      } catch (NamingException err) {
      return "";//验证失败返回空
      }

      }

 

posted @ 2017-03-31 11:12  相忘谁先忘  阅读(1155)  评论(0编辑  收藏  举报