package th.co.muangthai.endrprint.util; import org.apache.log4j.Logger; import org.hibernate.SQLQuery; import org.hibernate.Session; import th.co.muangthai.endrprint.dao.serviceImp.CommonServiceImp; import th.co.muangthai.endrprint.dao.serviceInterface.CommonServiceInterface; import th.co.muangthai.endrprint.model.bean.form.master.EndrUserLoginForm; import th.co.muangthai.endrprint.model.bean.form.master.MstOperidTabForm; import th.co.muangthai.endrprint.model.bean.master.MstOperidTabBean; import th.co.muangthai.endrprint.model.hibernate.HibernateUtil; import th.co.muangthai.endrprint.util.ADAuthenticator.*; import javax.naming.Context; import javax.naming.NamingEnumeration; import javax.naming.NamingException; import javax.naming.directory.Attributes; import javax.naming.directory.SearchControls; import javax.naming.directory.SearchResult; import javax.naming.ldap.InitialLdapContext; import javax.naming.ldap.LdapContext; import java.math.BigDecimal; import java.util.Date; import java.util.Hashtable; import java.util.List; /** * Created by IntelliJ IDEA. * User: Huato * Date: 8/14/12 * Time: 1:31 PM * To change this template use File | Settings | File Templates. */ public class ADAuthenticator { private final static String CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory"; private final static String AUTHENTICATION = "simple"; private final static String SEARCH_BASE = "DC=muangthai,DC=co,DC=th"; private final static String PROVIDER_URL = "ldap://10.1.0.5:389"; private final static String ATTRIBUTE_FOR_USER = "sAMAccountName"; //private final static String DN = "CN=Administrator,CN=Users,DC=muangthai,DC=co,DC=th"; private final static String DOMAIN = "muangthai"; public final String[] ATTRIBUTES = { "name", "physicalDeliveryOfficeName"}; private static Logger log = Logger.getLogger(ADAuthenticator.class); public static EndrUserLoginForm authenticate(String userId, String password) { try { String searchFilter = "(&(objectClass=person)(" + ATTRIBUTE_FOR_USER + "=" + userId + "))"; // String searchFilter = ATTRIBUTE_FOR_USER + "=" + userId; SearchControls searchCtls = new SearchControls(); //searchCtls.setReturningAttributes(ATTRIBUTES); searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); //Integer.parseInt("r"); Hashtable environment = new Hashtable(); environment.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY); environment.put(Context.PROVIDER_URL, PROVIDER_URL); environment.put(Context.SECURITY_AUTHENTICATION, AUTHENTICATION); environment.put(Context.SECURITY_PRINCIPAL, DOMAIN + "\\" + userId); environment.put(Context.SECURITY_CREDENTIALS, password); EndrUserLoginForm userBean = null; LdapContext ctxGC = new InitialLdapContext(environment, null); NamingEnumeration answer = ctxGC.search(SEARCH_BASE, searchFilter, searchCtls); while (answer.hasMoreElements()) { SearchResult sr = (SearchResult) answer.next(); Attributes attributes = sr.getAttributes(); userBean = new EndrUserLoginForm(); userBean.setUserId(userId); userBean.setUserPassword(password); userBean.setFirstname(attributes.get("cn") == null ? null : attributes.get("cn").get().toString()); //userBean.setFirstname(attributes.get("name") == null ? null :attributes.get("name").toString()); } return userBean; } catch (NamingException namingException) { namingException.printStackTrace(); } return null; } public static MstOperidTabForm queryAllUser(String userId, String password) { String searchFilter = "(&(objectClass=user))"; // String searchFilter = "(&(objectClass=person)(" + ATTRIBUTE_FOR_USER + "=mtl83200))"; // String searchFilter = "(&(objectClass=person)(" + ATTRIBUTE_FOR_USER + "=" + userId + "))"; // String searchFilter = ATTRIBUTE_FOR_USER + "=" + userId; SearchControls searchCtls = new SearchControls(); //searchCtls.setReturningAttributes(ATTRIBUTES); searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); // searchCtls.setCountLimit(10000); Hashtable environment = new Hashtable(); environment.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY); environment.put(Context.PROVIDER_URL, PROVIDER_URL); environment.put(Context.SECURITY_AUTHENTICATION, AUTHENTICATION); environment.put(Context.SECURITY_PRINCIPAL, DOMAIN + "\\" + userId); environment.put(Context.SECURITY_CREDENTIALS, password); MstOperidTabForm userBean = new MstOperidTabForm(); try { LdapContext ctxGC = new InitialLdapContext(environment, null); NamingEnumeration answer = ctxGC.search("DC=muangthai,DC=co,DC=th", searchFilter, searchCtls); int count = 1; Date now = new Date(); while (answer.hasMoreElements()) { SearchResult sr = (SearchResult) answer.next(); Attributes attributes = sr.getAttributes(); userBean.setUserid(userId); userBean.setFirstName(attributes.get("cn") == null ? null : attributes.get("cn").get().toString()); userId = (String)attributes.get("sAMAccountName").get(); if ("matching".equals(userId)) { } CommonServiceInterface userService = new CommonServiceImp(); // if (userId != null && (userId.startsWith("MTL") || userId.startsWith("mtl") // || userId.startsWith("mtg") || userId.startsWith("MTG") // )) if (1 == 1) { userId = userId.toLowerCase(); String userName = (String)attributes.get("cn").get(); MstOperidTabBean user = (MstOperidTabBean)userService.searchClassByPK(new MstOperidTabBean(),userId.toLowerCase()); if (user == null) { user = new MstOperidTabBean(); user.setOperid(""); user.setUserid(userId); user.setIdentifield(null); user.setOperName(userName); user.setLicienseName(userName); user.setDepcode(null); user.setDepartment(null); user.setTeam(null); user.setFlag(null); userService.updateTable(user); log.info("insert user id : " + userId + " : " + userName); } else { user.setOperName(userName); userService.updateTable(user); log.info("update user id : " + userId + " : " + userName); } //System.out.println(user); } } return userBean; } catch (NamingException namingException) { namingException.printStackTrace(); } return null; } public static MstOperidTabForm queryAllUser2(String userId, String password, String userUpdate) { // String searchFilter = "(&(objectClass=user))"; //String searchFilter = "(&(objectClass=person)(" + ATTRIBUTE_FOR_USER + "=mtl83200))"; String searchFilter = "(&(objectClass=person)(" + ATTRIBUTE_FOR_USER + "=" + userUpdate + "))"; // String searchFilter = ATTRIBUTE_FOR_USER + "=" + userId; SearchControls searchCtls = new SearchControls(); //searchCtls.setReturningAttributes(ATTRIBUTES); searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); // searchCtls.setCountLimit(10000); Hashtable environment = new Hashtable(); environment.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY); environment.put(Context.PROVIDER_URL, PROVIDER_URL); environment.put(Context.SECURITY_AUTHENTICATION, AUTHENTICATION); environment.put(Context.SECURITY_PRINCIPAL, DOMAIN + "\\" + userId); environment.put(Context.SECURITY_CREDENTIALS, password); MstOperidTabForm userBean = new MstOperidTabForm(); try { LdapContext ctxGC = new InitialLdapContext(environment, null); NamingEnumeration answer = ctxGC.search("DC=muangthai,DC=co,DC=th", searchFilter, searchCtls); int count = 1; Date now = new Date(); while (answer.hasMoreElements()) { SearchResult sr = (SearchResult) answer.next(); Attributes attributes = sr.getAttributes(); userBean.setUserid(userId); userBean.setFirstName(attributes.get("cn") == null ? null : attributes.get("cn").get().toString()); userId = (String)attributes.get("sAMAccountName").get(); if ("matching".equals(userId)) { } } return userBean; } catch (NamingException namingException) { namingException.printStackTrace(); } return null; } public static void main(String[] args) throws Exception { // String userId = "cadministrator"; // String password = "$t@rPlatt1nuM"; // String userId = "matching"; // String password = "12345"; String filterName = "anupong"; String password = "anupong_h"; ADAuthenticator ldap = new ADAuthenticator(); MstOperidTabBean att = null; ldap.queryAllUser(filterName, password); Session session = HibernateUtil.getCurrentSession(); String sql = "select * from user_login a \n" + " where user_name is null "; SQLQuery query = session.createSQLQuery(sql); query.addEntity(MstOperidTabBean.class); List list = query.list(); for (MstOperidTabBean bean : list) { ldap.queryAllUser2(filterName, password, bean.getUserid()); } if (att == null) { } else { try { int count = 1; // for (NamingEnumeration e = att.getAll(); e.hasMore(); ) { // String tempStr = e.next().toString(); // System.out.println("[" +(count++)+ "] " + tempStr); // } // System.out.println(); } catch (Exception e) { e.printStackTrace(); } // System.out.println("[FOUND] " + att.get("name").toString()); // System.out.println("[FOUND] " + (VSMUtil.isNotEmpty(att.get("Description")) ? att.get("Description").toString() : "Description:")); } } }