private static ResourceBundle bundle = ResourceBundle.getBundle("/properties/connection");
private static String url = bundle.getString("ldapUrl");
private static String principal = bundle.getString("ldapUser");
private static String password = bundle.getString("ldapPwd");
private static String baseDir = bundle.getString("ldapBaseDir");
@Autowired
private Log_ log;
public boolean checkADLDSLdapUserInfo(String userId,String password) throws NamingException {
boolean falg=false;
Hashtable<String,String> hash = new Hashtable<String,String>();
hash.put("java.naming.factory.initial","com.sun.jndi.ldap.LdapCtxFactory");
hash.put(Context.PROVIDER_URL, url);
hash.put(Context.SECURITY_AUTHENTICATION, "simple");
hash.put(Context.REFERRAL, "follow");
hash.put(Context.SECURITY_PRINCIPAL, userId);
hash.put(Context.SECURITY_CREDENTIALS, password);
SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
LdapContext ldapContext=null;
try {
ldapContext = new InitialLdapContext(hash, null);
falg=true;
} catch (NamingException e) {
log.service().error("User authentication failure");
log.service().error("ldap namingException message -----"+e.getMessage());
throw e;
}finally{
colseLdapConn(ldapContext);
}
return falg;
}
private void colseLdapConn(LdapContext ldapContext) {
try {
if(null!=ldapContext){
ldapContext.close();
}
} catch (NamingException e) {
log.service().info("ldap close exception message -----"+e.toString());
}
}
public String getUserName(String userId) throws NamingException{
InitialDirContext ctx = null;
try{
Hashtable<String,String> hash = new Hashtable<String,String>();
hash.put("java.naming.factory.initial","com.sun.jndi.ldap.LdapCtxFactory");
hash.put(Context.PROVIDER_URL, "ldap://ip:port");
hash.put(Context.SECURITY_AUTHENTICATION, "simple");
hash.put(Context.SECURITY_PRINCIPAL, "xxx user");
hash.put(Context.SECURITY_CREDENTIALS, "password");
hash.put(Context.REFERRAL, "follow");
ctx = new InitialLdapContext(hash, null);
String[] userAttrs = {
"sn", "sAMAccountName","displayName", "givenName",
"mail", "distinguishedName",
"memberOf","department","title","telephoneNumber","company"};
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(userAttrs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String baseDir = "OU=XX,DC=XX,DC=com";
SearchResult result = (SearchResult) ctx.search(baseDir,"sAMAccountName=" + userId, ctls).next();
Attributes attrs = result.getAttributes();
return attrs.get("displayName").get(0).toString();
}
catch (NamingException e) {
log.service().error("ldap namingException message -----"+e.getMessage());
}
finally{
if(ctx!=null){
ctx.close();
}
}
return StringUtils.EMPTY;
}
public List<String> getEmpGroups(String userId) throws NamingException {
InitialDirContext ctx = null;
try {
Hashtable<String, String> hash = new Hashtable<String, String>();
hash.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
hash.put(Context.SECURITY_AUTHENTICATION, "simple");
hash.put(Context.PROVIDER_URL, url);
hash.put(Context.SECURITY_PRINCIPAL, principal);
hash.put(Context.SECURITY_CREDENTIALS, password);
hash.put(Context.REFERRAL, "follow");
ctx = new javax.naming.ldap.InitialLdapContext(hash, null);
String[] userAttrs = { "memberOf" };
SearchControls ctls = new SearchControls();
ctls.setReturningAttributes(userAttrs);
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
SearchResult result = (SearchResult) ctx.search(baseDir, "cn=" + userId, ctls).next();
Attribute attr = result.getAttributes().get("memberOf");
List<String> groupList = new ArrayList<String>();
for (int i = 0; i < attr.size(); i++) {
groupList.add(attr.get(i).toString().split(",")[0].split("=")[1]);
}
return groupList;
}
catch (Exception e) {
throw e;
}finally{
if(ctx!=null){
ctx.close();
}
}
}