加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

JAVA 使用LDAP验证用户密码

(2012-10-24 09:29:58)
标签:

it

java

分类: JAVA

import java.security.MessageDigest;
import java.util.Hashtable;

import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.Control;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

 

 

public class Rz {
    private String URL = "ldap://10.50.143.19:389/";
    private String BASEDN = "uid=jeff,ou=people,dc=dlw,dc=com";
    private String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
    private LdapContext ctx = null;
    private Control[] connCtls = null;

 

    //构造,连接LDAP
    public Rz(){
        Hashtable 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());
        }
    }
   
   //根据email获取DN
    private String getUserDN(String email) {
        String userDN = "";
       
        try {
            SearchControls constraints = new SearchControls();
            constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
            NamingEnumeration en = ctx.search("", "mail=" + email, 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);
        }
       
        if(userDN.startsWith(",")){
            userDN = userDN.substring(1);
        }
       
        return userDN;
    }

   

    //通过LDAP 验证账号密码
    public boolean authenricate(String ID, String password) {
        boolean valide = false;
        String userDN = getUserDN(ID);

        try {
            ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, userDN);
            ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
            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) {
        Rz rz = new Rz();
       
        boolean ok =  rz.authenricate("jeff@123.com", "1234");
       
        if(ok){
            System.out.println("success");
        }else{
            System.out.println("field");
        }
       
        //TEST LDAP密码MD5加密,先MD5,再BASE64
        String pass = "123";
        byte[] byteArray = null;
       
        try {
           
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.reset(); 
            md.update(pass.getBytes("UTF-8")); 
           
            byteArray = md.digest(); 

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

       String s = (new sun.misc.BASE64Encoder()).encode(byteArray);
       System.out.println(s);//{md5}ICy5YqxZB1uWSwcVLSNLcA==
    }

}


http://www.vimi.hk/theme/default/img/logo1.png使用LDAP验证用户密码" TITLE="JAVA 使用LDAP验证用户密码" />

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有