LDAP认证
来源:互联网 发布:申通快递淘宝价格表 编辑:程序博客网 时间:2024/05/22 06:56
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;
}
}
阅读全文
0 0
- LDAP认证
- LDAP 认证
- LDAP认证
- spring security LDAP 认证
- Spring Ldap 域认证
- C# LDAP认证登录
- LDAP 用户认证!
- C# LDAP认证登录
- django添加ldap认证
- C# LDAP认证登录
- zabbix 监控LDAP认证
- 各种ldap认证
- nginx 搭建LDAP认证
- openvpn+ldap认证
- jenkins ldap 认证配置
- LDAP认证SVN
- 安全之LDAP认证
- Apache使用LDAP认证
- 重写toString方法 打印对象为json
- springcloud(一):大话Spring Cloud
- 欢迎使用CSDN-markdown编辑器
- linux查看日志文件内容命令sed、cat、tac、more、less、head、tail、echo 1、按时间查询 sed -n '/2017-09-20 14:00:/,/2017-
- PHP语言学习基础
- LDAP认证
- springcloud(二):注册中心Eureka
- 8266做websocket server
- Spring EnableAutoConfigurationImportSelector 是如何工作的 ?
- Maven简介与Maven安装
- 获取微信小程序的xml的值
- 文章标题
- 集合之--集合中的类
- PHP与JS中的math.random()对应方法切换