善用LDAP来做登陆验证(Java 中级)
首先关于是什么是LDAP,请参考 LADP_WIKI
Java 给我们已经提供了Ldap 登陆的API,所以我们就基于Java原生的LDAP 接口进行。
首先,我们需要定义准备LDAP 相关的常量
public static final String SECURITY_AUTHURICATION = "securityAuthurication"; public static final String LDAP_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory"; public static final String SECTION = "login"; public static final String SERVER = "server"; public static final String AMDIN_NAME = "username"; public static final String ADMIN_PWD = "password"; public static final String ACCOUNT_BASE = "accountBase"; public static final String REFERRAL = "referral"; public static final String SEARCH_SCOPE = "searchScope"; public static final String SEARCH_PATTERN = "searchPattern"; public static final String VALUES_SPLITOR = ","; public static final String SECURITY_AUTHURICATION = "securityAuthurication"; public static final String LDAP_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
Hashtable<String, String> env = new Hashtable<String, String>(); // 定义查询属性: env.put(Context.INITIAL_CONTEXT_FACTORY, LDAP_CONTEXT_FACTORY); env.put(Context.PROVIDER_URL, server); env.put(Context.REFERRAL, referral); env.put(Context.SECURITY_AUTHENTICATION, securityAuthurication); env.put(Context.SECURITY_PRINCIPAL, adminName); env.put(Context.SECURITY_CREDENTIALS, adminPwd);