<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 閱讀(2440) 評論(1)  編輯  收藏 所屬分類: JNDI

    FeedBack:
    # re: LDAP實例
    2007-11-18 11:20 | 雪浪
    真的感謝你的分享與給與!!!  回復  更多評論
      
    主站蜘蛛池模板: 亚洲伊人久久综合中文成人网| 美女被免费喷白浆视频| 亚洲?V乱码久久精品蜜桃| 亚洲乱理伦片在线观看中字| 成人最新午夜免费视频| 亚洲精品9999久久久久无码| 国产精品jizz在线观看免费| 一级做a爰片性色毛片免费网站| 亚洲国产精品一区二区第一页免| 全部一级一级毛片免费看| 亚洲一级片内射网站在线观看| 怡红院免费的全部视频| 91亚洲自偷手机在线观看| 97碰公开在线观看免费视频| 在线aⅴ亚洲中文字幕| 日本免费v片一二三区| 亚洲国产免费综合| 亚洲电影国产一区| 成人免费观看一区二区| 亚洲精品精华液一区二区| 中文字幕亚洲日韩无线码| 久久大香伊焦在人线免费| 久久精品国产99国产精品亚洲| 日本高清免费网站| 成人免费一区二区三区| 精品无码一区二区三区亚洲桃色| 午夜宅男在线永久免费观看网| 日本永久免费a∨在线视频| 久久91亚洲精品中文字幕| 青青青国产在线观看免费网站| 在线观看国产一区亚洲bd| 亚洲老妈激情一区二区三区| 最近免费字幕中文大全视频| 亚洲乱码av中文一区二区| 国产亚洲精品va在线| 毛片a级毛片免费播放下载| 中文字幕免费在线看电影大全| 亚洲一级毛片免费观看| 亚洲片国产一区一级在线观看| 亚洲日本在线免费观看| 杨幂最新免费特级毛片|