ldap验证

import java.util.Hashtable;

import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
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;

/**
 * 用户登陆认证,LDAP跨域认证,通过LDAP对用户进行更新
 * 
 */
public class LdapCheck {
    private static LdapContext ctx = null;
    private static Control[] connCtls = null;

/**** 定义LDAP的基本连接信息 ******/
    // LDAP的连接地址(ldap://ip:port/)port默认为389
    private static String URL = “?”;
    // LDAP的根DN
    private static String BASEDN = “dc=?,dc=?,dc=?”;
    // LDAP的连接账号(身份认证管理平台添加的应用账号,应用账号格式:uid=?,ou=?,dc=???)
    private static String PRINCIPAL = “uid=?,ou=?,dc=?,dc=?,dc=?”;
    // LDAP的连接账号的密码(身份认证管理平台添加的应用账号的密码)
    private static String PASSWORD = “?”;

public static void main(String[] args) {
        // 登录用户的用户名和密码
        String username = “?”;
        String password = “?”;
        System.out.println(authenticate(username, password));
        closeCtx();
    }

// 校验用户名密码的方法
    public static boolean authenticate(String usr, String pwd) {
        boolean valide = false;
        if (pwd == null || pwd == “”)
            return false;
        if (ctx == null) {
            getCtx();
        }
        String userDN = getUserDN(usr);
        if ("".equals(userDN) || userDN == null) {
            return false;
        }
        try {
            ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, userDN);
            ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, pwd);
            ctx.reconnect(connCtls);
            valide = true;
        } catch (AuthenticationException e) {
            System.out.println(userDN + " is not authenticated");
            System.out.println(e.toString());
            valide = false;
        } catch (NamingException e) {
            System.out.println(userDN + " is not authenticated");
            valide = false;
        }
        return valide;
    }

public static void getCtx() {
        if (ctx != null) {
            return;
        }
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, “com.sun.jndi.ldap.LdapCtxFactory”);
        env.put(Context.PROVIDER_URL, URL + BASEDN);
        env.put(Context.SECURITY_AUTHENTICATION, “simple”);
        env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL);
        env.put(Context.SECURITY_CREDENTIALS, PASSWORD);
        try {
            // 链接ldap
            ctx = new InitialLdapContext(env, connCtls);
        } catch (javax.naming.AuthenticationException e) {
            System.out.println("Authentication faild: " + e.toString());
        } catch (Exception e) {
            System.out.println("Something wrong while authenticating: " + e.toString());
        }
    }

public static void closeCtx() {
        try {
            if (ctx != null)
                ctx.close();
        } catch (NamingException ex) {
        }
    }

public static String getUserDN(String uid) {
        String userDN = “”;
        try {
            SearchControls constraints = new SearchControls();
            constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
            NamingEnumeration<?> en = ctx.search("", “uid=” + uid, constraints);
            if (en == null) {
                System.out.println(“Have no NamingEnumeration.”);
            }
            if (!en.hasMoreElements()) {
                System.out.println(“Have no element.”);
            }
            while (en != null && en.hasMoreElements()) {
                Object obj = en.nextElement();
                if (obj instanceof SearchResult) {
                    SearchResult si = (SearchResult) obj;
                    userDN += si.getName();
                    userDN += “,” + BASEDN;
                } else {
                    System.out.println(obj);
                }
                System.out.println();
            }
        } catch (Exception e) {
            System.out.println(“Exception in search():” + e);
        }

return userDN;
    }
}
————————————————
版权声明:本文为CSDN博主「Missisy」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_31461919/article/details/81112489

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在MySQL中,可以使用LDAP(Lightweight Directory Access Protocol)协议来进行身份验证,以增强数据库的安全性。要启用LDAP验证,需要进行以下步骤: 1. 安装LDAP模块: ``` sudo apt-get install libldap2-dev ``` 2. 配置LDAP文件`/etc/ldap.conf`(或`/etc/openldap/ldap.conf`),添加以下内容: ``` BASE dc=example,dc=com URI ldap://ldap.example.com BINDDN cn=admin,dc=example,dc=com BINDPW mypassword ``` 其中,`BASE`指定LDAP的基础DN,`URI`指定LDAP服务器的URI,`BINDDN`和`BINDPW`指定管理员的用户名和密码。 3. 修改MySQL服务器的配置文件`/etc/mysql/mysql.conf.d/mysqld.cnf`,启用LDAP验证: ``` [mysqld] plugin-load=auth_ldap_simple.so ldap-auth-config=/etc/mysql/ldap.cnf ``` 该配置文件指定了启用LDAP验证,并指定了LDAP配置文件的路径。 4. 创建LDAP配置文件`/etc/mysql/ldap.cnf`,添加以下内容: ``` server_host = ldap.example.com server_port = 389 search_base = dc=example,dc=com bind_dn = cn=admin,dc=example,dc=com bind_password = mypassword user_attribute = uid user_filter = (uid=%s) ``` 其中,`server_host`和`server_port`指定LDAP服务器的主机名和端口号,`search_base`指定LDAP搜索的基本DN,`bind_dn`和`bind_password`指定管理员的用户名和密码,`user_attribute`指定用户的属性,`user_filter`指定LDAP搜索用户的过滤条件。 5. 重启MySQL服务器,使配置生效: ``` systemctl restart mysql ``` 通过以上步骤,即可启用LDAP验证,增强MySQL数据库的安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值