<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)論
      
    主站蜘蛛池模板: 免费人成视频在线| 亚洲一区在线视频观看| 亚洲大香人伊一本线| 日韩毛片免费一二三| 免费黄色小视频网站| 2022年亚洲午夜一区二区福利| 一个人晚上在线观看的免费视频| 欧美三级在线电影免费| 黄色网址免费在线| 天天操夜夜操免费视频| 亚洲色偷精品一区二区三区| **aaaaa毛片免费| 亚洲美女aⅴ久久久91| 四虎永久在线免费观看| 亚洲精品无码av片| 最新69国产成人精品免费视频动漫 | 国产亚洲美女精品久久久久| 日韩伦理片电影在线免费观看| 亚洲乱理伦片在线观看中字| 日韩一级在线播放免费观看| 免费久久人人爽人人爽av| 久久亚洲国产成人精品无码区| 一级做a爱过程免费视频高清| 亚洲成a人片在线观看中文app| 精品亚洲成α人无码成α在线观看| 2022久久国产精品免费热麻豆| 免费在线观看自拍性爱视频| 亚洲国产精品久久人人爱| 久久精品免费电影| 亚洲一区二区三区夜色| 114一级毛片免费| 亚洲欧美日韩中文字幕在线一区 | 亚洲欧洲另类春色校园网站| 成人激情免费视频| 国产亚洲精品2021自在线| 亚洲的天堂av无码| 成年性午夜免费视频网站不卡| 日韩免费视频一区二区| 一级毛片免费毛片毛片| 亚洲Av永久无码精品黑人| 亚洲精品无码av天堂|