问题描述
我用java连接LDAP做用户认证,LDAP里面的密码是经过DES(crypt)加密过的,认证就不能通过,如果LDAP里面的密码是SHA方式加密的,认证就能通过,请问各位高手这是为什么啊?程序如下:publicclassUserAuthenticate{privateStringURL="LDAP://10.72.0.50:389/";privateStringBASEDN="dc=hntobacco,dc=com";privateStringFACTORY="com.sun.jndi.ldap.LdapCtxFactory";privateLdapContextctx=null;privateHashtableenv=null;privateControl[]connCtls=null;privatevoidLDAP_connect(){env=newHashtable();env.put(Context.INITIAL_CONTEXT_FACTORY,FACTORY);env.put(Context.PROVIDER_URL,URL+BASEDN);//LDAPserverenv.put(Context.SECURITY_AUTHENTICATION,"simple");//此处若不指定用户名和密码,则自动转换为匿名登录try{ctx=newInitialLdapContext(env,connCtls);}catch(javax.naming.AuthenticationExceptione){System.out.println("Authenticationfaild:"+e.toString());}catch(Exceptione){System.out.println("Somethingwrongwhileauthenticating:"+e.toString());}}privateStringgetUserDN(Stringuid){StringuserDN="";LDAP_connect();try{SearchControlsconstraints=newSearchControls();constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);NamingEnumerationen=ctx.search("","uid="+uid,constraints);//TheUIDyouaregoingtoquery,*meansallnodesif(en==null){System.out.println("HavenoNamingEnumeration.");}if(!en.hasMoreElements()){System.out.println("Havenoelement.");}while(en!=null&&en.hasMoreElements()){//maybemorethanoneelementObjectobj=en.nextElement();if(objinstanceofSearchResult){SearchResultsi=(SearchResult)obj;userDN+=si.getName();userDN+=","+BASEDN;}else{System.out.println(obj);}System.out.println();}}catch(Exceptione){System.out.println("Exceptioninsearch():"+e);}returnuserDN;}publicbooleanauthenricate(StringID,Stringpassword){booleanvalide=false;StringuserDN=getUserDN(ID);//Stringsecretmthod="DES";try{ctx.addToEnvironment(Context.SECURITY_PRINCIPAL,userDN);ctx.addToEnvironment(Context.SECURITY_CREDENTIALS,password);//ctx.addToEnvironment(Context.REFERRAL,secretmthod);ctx.reconnect(connCtls);System.out.println(userDN+"isauthenticated");valide=true;}catch(AuthenticationExceptione){System.out.println(userDN+"isnotauthenticated");System.out.println(e.toString());valide=false;}catch(NamingExceptione){System.out.println(userDN+"isnotauthenticated");valide=false;}returnvalide;}publicstaticvoidmain(Stringargs[]){UserAuthenticateua=newUserAuthenticate();ua.authenricate("liudb","2");}}
解决方案
解决方案二:
该回复于2009-07-17 09:26:43被版主删除