<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 | 雪浪
    真的感謝你的分享與給與!!!  回復  更多評論
      
    主站蜘蛛池模板: 亚洲无人区午夜福利码高清完整版 | 亚洲日韩精品无码专区| 性无码免费一区二区三区在线| 亚洲中文字幕无码一久久区| 日本视频免费观看| 久久久久久亚洲精品不卡| 中文字幕免费在线视频| 亚洲一区精品无码| 免费观看男人吊女人视频| 亚洲av永久无码精品漫画 | 国产亚洲精品拍拍拍拍拍| jyzzjyzz国产免费观看| 亚洲色成人网站WWW永久| 两个人看的www免费视频| 亚洲国产精品不卡在线电影| 成人免费福利视频| 亚洲精品乱码久久久久久V| heyzo亚洲精品日韩| 日韩精品无码免费专区网站 | 日韩在线a视频免费播放| 美女免费视频一区二区| 亚洲最大AV网站在线观看| 中文字幕久精品免费视频| 亚洲网站在线播放| 永久免费看mv网站入口| 国产成人无码精品久久久免费| 久久国产精品亚洲综合| 99视频全部免费精品全部四虎| 亚洲国产美女精品久久久| 亚洲日韩国产精品乱| 无码人妻AV免费一区二区三区| 国产成+人+综合+亚洲专| 亚洲国产成人精品女人久久久| a色毛片免费视频| 国产成人亚洲合集青青草原精品| 免费va人成视频网站全| 久久国产精品免费看| 亚洲aⅴ无码专区在线观看春色| 国产国拍亚洲精品福利| 我的小后妈韩剧在线看免费高清版 | 亚洲AV无码专区亚洲AV伊甸园|