package com.sn.ssm.utils;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
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 LdapUtils {
private String URL = "ldap://XXXXXXX:389/";
private String BASEDN = "dc=neusoft,dc=internal";
private String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
private LdapContext ctx = null;
private Hashtable<String, String> env = null;
private Control[] connCtls = null;
public static void main(String[] args) {
LdapUtils lp = new LdapUtils();
boolean Lcontect=lp.LDAP_connect("lis","Neu,123.LS");
boolean Lauthenricate =lp.authenricate("public_ldap_user","Neu,123.");
Map<String,String> userMessage =lp.getUserMessage("zhao.shd");
for(Map.Entry<String, String> entry:userMessage.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
//System.out.println(userMessage.get("msDS-PhoneticDisplayName"));
}
public boolean LDAP_connect(String userName,String passWord){
env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY,FACTORY);
env.put(Context.PROVIDER_URL, URL+BASEDN);//LDAP server
env.put(Context.SECURITY_AUTHENTICATION, "simple");
//此处若不指定用户名和密码,则自动转换为匿名登录
env.put(Context.SECURITY_PRINCIPAL, "cn="+userName+",ou=people,dc=neusoft,dc=internal");
env.put(Context.SECURITY_CREDENTIALS, passWord);
try{
ctx = new InitialLdapContext(env,connCtls);
return true;
}catch(javax.naming.AuthenticationException e){
System.out.println("Authentication faild: "+e.toString());
return false;
}catch(Exception e){
System.out.println("Something wrong while authenticating: "+e.toString());
return false;
}
}
public Map<String,String> getUserMessage(String email){
Map<String,String> result_map=new HashMap<>();
try{
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration en = ctx.search("", "mail="+email+"@neusoft.com", 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;
Attributes at = si.getAttributes();
NamingEnumeration ane = at.getAll();
while (ane.hasMore()) {
Attribute attr = (Attribute) ane.next();
String attrType = attr.getID();
NamingEnumeration values = attr.getAll();
Vector vals = new Vector();
// Another NamingEnumeration object, this time
// to iterate through attribute values.
while (values.hasMore()) {
Object oneVal = values.nextElement();
if (oneVal instanceof String) {
result_map.put(attrType, oneVal.toString());
//System.out.println(attrType + ": " + (String) oneVal);
} else {
System.out.println(attrType + ": " + new String((byte[]) oneVal));
}
}
}
}
else{
System.out.println(obj);
}
System.out.println();
}
}catch(Exception e){
System.out.println("Exception in search():"+e);
}
return result_map;
}
public boolean authenricate(String eamil,String password){
boolean valide = false;
//String userDN = getUserDN(ID);
try {
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL,"cn="+eamil+",ou=people,dc=neusoft,dc=internal");
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS,password);
ctx.reconnect(connCtls);
System.out.println(eamil + " is authenticated");
valide = true;
}catch (AuthenticationException e) {
System.out.println(eamil + " is not authenticated");
System.out.println(e.toString());
valide = false;
}catch (NamingException e) {
System.out.println(eamil + " is not authenticated");
valide = false;
}
return valide;
}
}
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
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 LdapUtils {
private String URL = "ldap://XXXXXXX:389/";
private String BASEDN = "dc=neusoft,dc=internal";
private String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
private LdapContext ctx = null;
private Hashtable<String, String> env = null;
private Control[] connCtls = null;
public static void main(String[] args) {
LdapUtils lp = new LdapUtils();
boolean Lcontect=lp.LDAP_connect("lis","Neu,123.LS");
boolean Lauthenricate =lp.authenricate("public_ldap_user","Neu,123.");
Map<String,String> userMessage =lp.getUserMessage("zhao.shd");
for(Map.Entry<String, String> entry:userMessage.entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue());
}
//System.out.println(userMessage.get("msDS-PhoneticDisplayName"));
}
public boolean LDAP_connect(String userName,String passWord){
env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY,FACTORY);
env.put(Context.PROVIDER_URL, URL+BASEDN);//LDAP server
env.put(Context.SECURITY_AUTHENTICATION, "simple");
//此处若不指定用户名和密码,则自动转换为匿名登录
env.put(Context.SECURITY_PRINCIPAL, "cn="+userName+",ou=people,dc=neusoft,dc=internal");
env.put(Context.SECURITY_CREDENTIALS, passWord);
try{
ctx = new InitialLdapContext(env,connCtls);
return true;
}catch(javax.naming.AuthenticationException e){
System.out.println("Authentication faild: "+e.toString());
return false;
}catch(Exception e){
System.out.println("Something wrong while authenticating: "+e.toString());
return false;
}
}
public Map<String,String> getUserMessage(String email){
Map<String,String> result_map=new HashMap<>();
try{
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration en = ctx.search("", "mail="+email+"@neusoft.com", 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;
Attributes at = si.getAttributes();
NamingEnumeration ane = at.getAll();
while (ane.hasMore()) {
Attribute attr = (Attribute) ane.next();
String attrType = attr.getID();
NamingEnumeration values = attr.getAll();
Vector vals = new Vector();
// Another NamingEnumeration object, this time
// to iterate through attribute values.
while (values.hasMore()) {
Object oneVal = values.nextElement();
if (oneVal instanceof String) {
result_map.put(attrType, oneVal.toString());
//System.out.println(attrType + ": " + (String) oneVal);
} else {
System.out.println(attrType + ": " + new String((byte[]) oneVal));
}
}
}
}
else{
System.out.println(obj);
}
System.out.println();
}
}catch(Exception e){
System.out.println("Exception in search():"+e);
}
return result_map;
}
public boolean authenricate(String eamil,String password){
boolean valide = false;
//String userDN = getUserDN(ID);
try {
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL,"cn="+eamil+",ou=people,dc=neusoft,dc=internal");
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS,password);
ctx.reconnect(connCtls);
System.out.println(eamil + " is authenticated");
valide = true;
}catch (AuthenticationException e) {
System.out.println(eamil + " is not authenticated");
System.out.println(e.toString());
valide = false;
}catch (NamingException e) {
System.out.println(eamil + " is not authenticated");
valide = false;
}
return valide;
}
}