ldap验证

ldap验证的工具类,具体的一些参数注释请参照相关文档。


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.directory.*;
import javax.naming.ldap.InitialLdapContext;
import java.util.Hashtable;

/**
* Ldap验证工具类,验证用户名和密码是否存在ldap服务器中。
*
* @author 张国明 guomingzhang2008@gmail.com
* @version 2012-11-15 上午9:32
*/
public class LdapUtil {
private final Log log = LogFactory.getLog(getClass());
/**
* 连接url
*/
private static final String URL = "ldap://bj-dcd.zhanggmsoft.com:389";
/**
* 系统用户名
*/
private static final String PRINCIPAL = "t-bysystem";
/**
* 系统密码
*/
private static final String CREDENTIALS = "t-bysystem20120716";

private static LdapUtil instance = new LdapUtil();

private LdapUtil() {
}

public static LdapUtil getInstance() {
return instance;
}

/**
* 验证用户名和密码
*
* @param username 用户名
* @param password 密码
* @return 是否验证成功
* @throws Exception
*/
public boolean validateUser(String username, String password) throws Exception {
InitialDirContext initialContext = (InitialDirContext) getInitialContext();
try {
SearchControls sc = new SearchControls();
sc.setSearchScope(2);

NamingEnumeration iter = initialContext.search("dc=zhanggmsoft,dc=com", "(&(samaccountname=" + username + ")(objectclass=user))", sc);
if (iter.hasMoreElements()) {
SearchResult result = (SearchResult) iter.nextElement();
Attributes attributes = result.getAttributes();
Attribute att = attributes.get("distinguishedname");
String dn = (String) att.get();
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.PROVIDER_URL, URL);
env.put(Context.SECURITY_PRINCIPAL, dn);
env.put(Context.SECURITY_CREDENTIALS, password);
env.put(Context.REFERRAL, "follow");
InitialDirContext initialContext1 = new InitialDirContext(env);
try {
initialContext1.search(dn, null);
} catch (Exception e) {
log.error(e);
return false;
} finally {
initialContext1.close();
}
} else {
return false;
}
return true;
} catch (Exception e) {
log.error(e);
return false;
} finally {
initialContext.close();
}
}

private InitialContext getInitialContext() throws Exception {
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);
env.put(Context.REFERRAL, "follow");
env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL);
env.put(Context.SECURITY_CREDENTIALS, CREDENTIALS);
return new InitialLdapContext(env, null);
}

public static void main(String[] args) throws Exception {
String username = "querenjie";
String password = "querenjie20120809";
boolean validate = getInstance().validateUser(username, password);
System.out.println("validate = " + validate);
}
}
在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、付费专栏及课程。

余额充值