<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 23,comments - 12,trackbacks - 0


    import java.util.Properties;
    import javax.naming.*;  
    import javax.naming.directory.*;
    /*
     * Created on 2005-10-26
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */

     

     

    class LdapAuth
    {
    public static void main(String[] args)
    {
        //***************** user information to be authenticated ********************************
        //*****************Please modify the following three properties accordingly ************
        String ldapHost= "ldap://aaa:389"; //ldap host + port number
       String DN = "cn=admin,cn=users,dc=aa,dc=com,dc=cn";
      String password = "changeit" ; 
     
        Properties props = new Properties(); 
        props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");   

        props.put(Context.SECURITY_AUTHENTICATION, "simple");     //use simple authentication mechanism
        props.put(Context.SECURITY_PRINCIPAL, DN);   
        props.put(Context.SECURITY_CREDENTIALS, password); 
        props.put(Context.PROVIDER_URL, ldapHost); 

        long start = System.currentTimeMillis();
        long end=0;
        long time =0;

        try
            {
            System.out.println("authenticating"); 
            DirContext ctx = new InitialDirContext(props);
            System.out.println("authenticated");
            end = System.currentTimeMillis();
            time = end - start;
            System.out.println( "authentication takes = " + time + " millis");    
            System.out.println("successfully authenticate DN: "+DN);
         
            Attributes Atb = ctx.getAttributes("cn=username,ou=it,dc=aa,dc=com,dc=cn");
            NamingEnumeration enum = Atb.getAll();
            System.out.println("aa");
            while (enum.hasMore()) {
                Attribute attr = (Attribute)enum.next();
                System.out.println(attr);
            }

        }
            catch (Exception ex)
        {
            end = System.currentTimeMillis();
            time = end - start;
            System.out.println("Exception is "+ex.toString());
            ex.printStackTrace();
            System.out.println( "authentication takes = " + time + " millis");   
            System.out.println("fail to authenticate DN: "+DN);
        }
    }
    }







    adauth.java

    import java.lang.*;
    import java.util.Hashtable ;
    import javax.naming.*;
    import javax.naming.directory.* ;
    import javax.naming.ldap.*;
    import javax.naming.spi.*;
     
     public class ADAuth {
     
        //環(huán)境設(shè)定
        private Hashtable env = null;
     
        //目錄
        DirContext ctx = null;
     
        //是否能login
        boolean bLogin = false;
      
        //更改是否成功
        boolean getAttr = false;
       
     
      public ADAuth(String strId ,String strPassword ){
        env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://xxx.xxx.xxx.xxx:389");
        env.put(Context.SECURITY_AUTHENTICATION,"Simple");
        env.put(Context.SECURITY_PRINCIPAL,"cn="+strId+",cn=users,DC=EEE,DC=abc,DC=com,DC=tw");
        env.put(Context.SECURITY_CREDENTIALS , strPassword );
     }//end ADAuth()
     
      public ADAuth(){
        this( "Administrator" , "xxxxxxx" );
      }
     
      public boolean checkAuth(){
        
     
        try{
          
          System.out.println("------------------");
          System.out.println("連入LDAP--->");      
          ctx = new InitialDirContext(env);
          System.out.println("ok");
     
          bLogin = true ;
     
        } catch(javax.naming.AuthenticationException authe) {
          
          System.out.println("失敗");
          bLogin = false ;
          
        } catch(Exception e) {
          
          System.out.println("失敗");
          System.out.println(e);
          
        } finally{
          
            try{
              ctx.close();
            
            }catch(Exception Ignore){}
          
        } 
        return bLogin ;
      }
      
      public boolean getAttribute(String a){
        
           //設(shè)定要取得的attribute
          String[] attrIds ={"displayName"};    
        
          try{
          
            ctx = new InitialDirContext(env);
            Attributes Atb = ctx.getAttributes("cn=users,DC=EEE,DC=abc,DC=com,DC=tw",attrIds);
            NamingEnumeration enum = Atb.getAll();
              while (enum.hasMore()) {
                  Attribute attr = (Attribute)enum.next();
                  System.out.println(attr);
              }
     
            System.out.println("------------------ok");
            getAttr = true;
                
        } catch(javax.naming.AuthenticationException authe) {
          
          System.out.println("失敗");      
            getAttr = false;
          
        } catch(Exception e) {
          
          
          System.out.println("失敗");
            getAttr = false;
                    System.out.println(e);
          
        } finally{
          
            try{
              ctx.close();
            
            }catch(Exception Ignore){}
        }
        return getAttr;
     }
    }
     
    
    posted on 2005-10-31 08:57 my java 閱讀(2440) 評(píng)論(1)  編輯  收藏 所屬分類(lèi): JNDI

    FeedBack:
    # re: LDAP實(shí)例
    2007-11-18 11:20 | 雪浪
    真的感謝你的分享與給與!!!  回復(fù)  更多評(píng)論
      
    主站蜘蛛池模板: 亚洲VA中文字幕不卡无码| 日韩精品视频免费网址| 国产美女亚洲精品久久久综合| 亚洲AV无码成人精品区狼人影院 | 免费人成网站在线观看10分钟| 色拍自拍亚洲综合图区| 99久久国产免费中文无字幕| 亚洲精品综合一二三区在线| 午夜免费福利片观看| 亚洲成年人电影网站| 拍拍拍又黄又爽无挡视频免费| 亚洲人精品亚洲人成在线| 成人免费无码大片a毛片软件| 亚洲国产精品久久久久秋霞小| 亚洲AV无码成人精品区大在线| 亚美影视免费在线观看| 一本久久a久久精品亚洲| 高清一区二区三区免费视频| 久久精品国产亚洲AV无码偷窥| 中文字幕无码视频手机免费看 | 国产精品视频永久免费播放| 亚洲精品无码国产片| 亚洲成a人片在线观看日本麻豆| 九九免费观看全部免费视频| 国产精品亚洲а∨无码播放| 免费在线观看视频网站| 亚洲人成欧美中文字幕| 亚洲日本va午夜中文字幕久久 | 亚洲欧美日韩中文二区| 亚洲精品国产va在线观看蜜芽| 中文字幕无码免费久久| 亚洲精品美女网站| 亚洲性日韩精品国产一区二区| 暖暖在线视频免费视频| 亚洲av乱码中文一区二区三区| 久久久久久A亚洲欧洲AV冫| 57pao国产成永久免费视频| 亚洲色成人四虎在线观看| 久久久久亚洲AV成人网人人网站| 84pao国产成视频免费播放| 免费国产va视频永久在线观看|