java LADP方式验证用户有效性

该博客介绍了如何在Java中使用LADP(轻量级目录访问协议)进行用户有效性验证。通过设置AD域IP、域名后缀、端口等参数,建立连接并执行简单安全级别的身份验证。当验证成功时返回'success',失败则返回'faild',并对可能出现的异常情况进行捕获和处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static String authentication(String userName,String passWord){


// TODO Auto-generated method stub
String flag;
String host = "*******";//AD域IP,必须填写正确
    String domain = "********";//域名后缀,例.@noker.cn.com
    String port = "389"; //端口,一般默认389
    String url = new String("ldap://" + host + ":" + port);//固定写法
    String user = userName.indexOf(domain) > 0 ? userName : userName
    + domain;//网上有别的方法,但是在我这儿都不好使,建议这么使用
    Hashtable env = new Hashtable();//实例化一个Env
    DirContext ctx = null;
    env.put(Context.SECURITY_AUTHENTICATION, "simple");//LDAP访问安全级别(none,simple,strong),一种模式,这么写就行
    env.put(Context.SECURITY_PRINCIPAL, user); //用户名
    env.put(Context.SECURITY_CREDENTIALS, passWord);//密码
    env.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.ldap.LdapCtxFactory");// LDAP工厂类
    env.put(Context.PROVIDER_URL, url);//Url
    try {
    ctx = new InitialDirContext(env);// 初始化上下文
    flag="success";
    } catch (AuthenticationException e) {
    System.out.println("身份验证失败!");
    flag="faild";
    e.printStackTrace();
    } catch (javax.naming.CommunicationException e) {
    System.out.println("AD域连接失败!");
    flag="faild";
    e.printStackTrace();
    } catch (Exception e) {
    System.out.println("身份验证未知异常!");
    flag="faild";
    e.printStackTrace();
    } finally{
    if(null!=ctx){
    try {
    ctx.close();
    ctx=null;
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    return flag;

}
Java中操作LDAP(轻量级目录访问协议)用户通常涉及使用JNDI(Java Naming and Directory Interface)API,这是一个标准库,用于提供对各种命名和目录服务的支持。以下是基本步骤: 1. **添加依赖**:确保在你的项目中已经包含了LDAP相关的jar包,比如`javax.naming`或更现代的`com.unboundid.ldapsdk`。 2. **配置环境**:在运行时,你需要提供LDAP服务器的URL、用户名、密码等连接信息。可以使用`InitialContext`对象初始化并设置属性。 ```java Properties env = new Properties(); env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory"); env.put("java.naming.provider.url", "ldap://yourldapserver.example.com"); env.put("java.naming.security.authentication", "simple"); env.put("java.naming.security.principal", "cn=manager,dc=example,dc=com"); env.put("java.naming.security.credentials", "yourpassword"); InitialContext context = new InitialContext(env); ``` 3. **查找用户**:你可以通过`NamingEnumeration`遍历结果,找到特定的用户。例如搜索某个邮箱地址。 ```java NamingEnumeration<SearchResult> results = context.search( "ou=People,dc=example,dc=com", "(mail=*@example.com)", // 搜索条件 new String[]{"*"}, // 返回所有属性 SearchControls.SUBTREE_SCOPE ); while (results.hasMore()) { SearchResult entry = results.next(); Attributes attrs = entry.getAttributes(); String distinguishedName = attrs.get("distinguishedName").get().toString(); // 用户DN } ``` 4. **修改或删除用户**:如果需要更新或删除用户,可以用`context.rename()`或`context.destroySubcontext()`方法。 ```java // 更新用户 context.rename(distinguishedName, "newDistinguishedName"); // 删除用户 context.destroySubcontext(distinguishedName); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值