我用java连接LDAP做用户认证,LDAP里面的密码是经过DES(crypt)加密过的,认证就不能通过,如果LDAP里面的密码是SHA方式加密的,认证就能通过,请问各位高手这是为什么啊?
程序如下:
public class UserAuthenticate {
private String URL = "LDAP://10.72.0.50:389/";
private String BASEDN = "dc=hntobacco,dc=com";
private String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
private LdapContext ctx = null;
private Hashtable env = null;
private Control[] connCtls = null;
private void LDAP_connect(){
env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,FACTORY);
env.put(Context.PROVIDER_URL, URL+BASEDN);//LDAP server
env.put(Context.SECURITY_AUTHENTICATION, "simple");
//此处若不指定用户名和密码,则自动转换为匿名登录
try{
ctx = new InitialLdapContext(env,connCtls);
}catch(javax.naming.AuthenticationException e){
System.out.println("Authentication faild: "+e.toString());
}catch(Exception e){
System.out.println("Something wrong while authenticating: "+e.toString());
}
}
private String getUserDN(String uid){
String userDN = "";
LDAP_connect();
try{
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration en = ctx.search("", "uid="+uid, constraints); //The UID you are going to query,* means all nodes
if(en == null){
System.out.println("Have no NamingEnumeration.");
}
if(!en.hasMoreElements()){
System.out.println("Have no element.");
}
while (en != null && en.hasMoreElements()){//maybe more than one element
Object obj = en.nextElement();
if(obj instanceof SearchResult){
SearchResult si = (SearchResult) obj;
userDN += si.getName();
userDN += "," + BASEDN;
}
else{
System.out.println(obj);
}
System.out.println();
}
}catch(Exception e){
System.out.println("Exception in search():"+e);
}
return userDN;
}
public boolean authenricate(String ID,String password){
boolean valide = false;
String userDN = getUserDN(ID);
// String secretmthod = "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 + " is authenticated");
valide = true;
}catch (AuthenticationException e) {
System.out.println(userDN + " is not authenticated");
System.out.println(e.toString());
valide = false;
}catch (NamingException e) {
System.out.println(userDN + " is not authenticated");
valide = false;
}
return valide;
}
public static void main(String args[]){
UserAuthenticate ua = new UserAuthenticate();
ua.authenricate("liudb", "2");
}
}
程序如下:
public class UserAuthenticate {
private String URL = "LDAP://10.72.0.50:389/";
private String BASEDN = "dc=hntobacco,dc=com";
private String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
private LdapContext ctx = null;
private Hashtable env = null;
private Control[] connCtls = null;
private void LDAP_connect(){
env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,FACTORY);
env.put(Context.PROVIDER_URL, URL+BASEDN);//LDAP server
env.put(Context.SECURITY_AUTHENTICATION, "simple");
//此处若不指定用户名和密码,则自动转换为匿名登录
try{
ctx = new InitialLdapContext(env,connCtls);
}catch(javax.naming.AuthenticationException e){
System.out.println("Authentication faild: "+e.toString());
}catch(Exception e){
System.out.println("Something wrong while authenticating: "+e.toString());
}
}
private String getUserDN(String uid){
String userDN = "";
LDAP_connect();
try{
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration en = ctx.search("", "uid="+uid, constraints); //The UID you are going to query,* means all nodes
if(en == null){
System.out.println("Have no NamingEnumeration.");
}
if(!en.hasMoreElements()){
System.out.println("Have no element.");
}
while (en != null && en.hasMoreElements()){//maybe more than one element
Object obj = en.nextElement();
if(obj instanceof SearchResult){
SearchResult si = (SearchResult) obj;
userDN += si.getName();
userDN += "," + BASEDN;
}
else{
System.out.println(obj);
}
System.out.println();
}
}catch(Exception e){
System.out.println("Exception in search():"+e);
}
return userDN;
}
public boolean authenricate(String ID,String password){
boolean valide = false;
String userDN = getUserDN(ID);
// String secretmthod = "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 + " is authenticated");
valide = true;
}catch (AuthenticationException e) {
System.out.println(userDN + " is not authenticated");
System.out.println(e.toString());
valide = false;
}catch (NamingException e) {
System.out.println(userDN + " is not authenticated");
valide = false;
}
return valide;
}
public static void main(String args[]){
UserAuthenticate ua = new UserAuthenticate();
ua.authenricate("liudb", "2");
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货