<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 {
     
        //環境設定
        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){
        
           //設定要取得的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 閱讀(2447) 評論(1)  編輯  收藏 所屬分類: JNDI

    FeedBack:
    # re: LDAP實例
    2007-11-18 11:20 | 雪浪
    真的感謝你的分享與給與!!!  回復  更多評論
      
    主站蜘蛛池模板: 亚洲成人动漫在线观看| 最近最新的免费中文字幕| 亚洲精品成人片在线观看精品字幕| 中文字幕无码免费久久| 亚洲一区二区三区久久| 四只虎免费永久观看| 99免费精品视频| 精品久久久久久久免费人妻| 一个人免费观看视频在线中文| 亚洲av日韩av天堂影片精品| 老司机精品免费视频| 中文字幕精品亚洲无线码一区应用| 99久久免费看国产精品| 亚洲a无码综合a国产av中文| 亚洲国产精品va在线播放| 午夜成人免费视频| 亚洲一卡2卡4卡5卡6卡在线99| 免费成人在线观看| 久久99国产综合精品免费| 深夜a级毛片免费无码| 亚洲欧洲日产专区| 狠狠亚洲狠狠欧洲2019| 一二三四在线播放免费观看中文版视频 | 亚洲va中文字幕| 91青青国产在线观看免费| 欧美亚洲精品一区二区| 亚洲无线电影官网| 亚洲日韩在线观看| 成年女性特黄午夜视频免费看| 青青操视频在线免费观看| 老子影院午夜伦不卡亚洲| 亚洲福利视频网站| 亚洲无线码一区二区三区| 国产精品免费看久久久无码| 麻豆高清免费国产一区| 国产真人无码作爱免费视频| 国产成人亚洲精品无码AV大片| 亚洲国产成人在线视频| 水蜜桃亚洲一二三四在线| 久久久久一级精品亚洲国产成人综合AV区| 欧美好看的免费电影在线观看|