知识
助手
最大化  清空记录  历史记录
   2300  
查询码: 00000244
Java实现AD域登录认证
来源:https://blog.csdn.net/Pnoter/article/details/52115898
作者: 系统管理员 于 2018年11月29日 发布在分类 / 技术研发 / LDAP ,于 2019年11月27日 编辑
认证 登录 验证 AD域 LDAP

package com.app;

import java.util.Hashtable;

import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class AdLogin {
    public static void main(String[] args) {
        String userName = "username";//AD域认证,用户的登录UserName
        String password = "";//AD域认证,用户的登录PassWord
        String host = "xxx.xxx.xxx.xxx";//AD域IP,必须填写正确
        String domain = "@xxx.xx";//域名后缀,例.@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);// 初始化上下文
            System.out.println("身份验证成功!");
        } catch (AuthenticationException e) {
            System.out.println("身份验证失败!");
            e.printStackTrace();
        } catch (javax.naming.CommunicationException e) {
            System.out.println("AD域连接失败!");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("身份验证未知异常!");
            e.printStackTrace();
        } finally{
            if(null!=ctx){
                try {
                    ctx.close();
                    ctx=null;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


0人参与


 历史版本

修改日期 修改人 备注
2019-11-27 23:45:43[当前版本] 系统管理员 API更新知识
2019-02-16 09:14:50 系统管理员 格式调整
2018-11-29 16:13:17 系统管理员 CREAT

wcp知识库系统-京ICP备15024440号-1 -V 5.1.3 -wcp