2023年6月21日发(作者:)
Java实现LDAP认证(上)Baidu脑残,把原来的空间改得不伦不类。所以把⼀些技术的东西挪到这⾥。我找到两种⽅法,⼤同⼩异,第⼀种是通过Spring,适合已经采⽤Spring的项⽬。⼀般来说⽤户名和密码都是保存在数据库中。现在有这个需求,⽤户名和密码是保存在M$的AD中。那么如何进⾏认证。我找到的⽅法有两种,⼀种是jcifs,另⼀种是Spring Security。这⾥⽤了Spring Security,参考了CAS SSO中LDAP验证的实现。⾸先建⼀个测试环境。在Server 2003中安装Domain,这个没啥说的,⽹上⽂章⼀⼤把。安装好以后,创建测试⽤的⽤户组(Team1)和两个⽤户(test,test1)。test⽤户属于组Team1,test1则不属于。如下图。Domain的名称是。创建好以后,在Eclipse中创建Java项⽬,加⼊以下的需要的Jar包:开始写代码。初始化部分,⼀些LDAP的参数设置。static { LdapContextSource cs = new LdapContextSource(); heEnvironmentProperties(false); ("ldap://192.168.1.200:389"); e("CN=Users,DC=dc,DC=testdc,DC=com"); henticationSource(new AuthenticationSource() { public String getCredentials() { return "
换句话说,上⾯的查询,可以这样理解。⾸先,我们通过Search Base的设置(setBase("CN=Users,DC=dc,DC=testdc,DC=com"))设置了查询的范围。然后上⾯的filter = "sAMAccountName=" + username; 设置了查询的条件。例如sAMAccountName=test的意思就是在"CN=Users,DC=dc,DC=testdc,DC=com"所表⽰的这么多对象中,查找sAMAccountName=test的对象。换句话说,就是查找登录的⽤户,在AD服务器中是否存在? 然后程序中继续判断: if (y()) { n("Search for " + filter + " returned 0 results."); return false; } if (() > 1) { n("Search for " + filter + " returned multiple results, which is not allowed."); return false; }不存在,或者多余⼀个存在,都是错误。返回;
for (final String dn : cns) { DirContext test = null; String finalDn = dn; try { n("Performing LDAP bind with credential: " + dn); test = textSource().getContext( finalDn, password); if (test != null) { return true; } } catch (final Exception e) { tackTrace(); } finally { if (test != null) { (); } } }如果存在,并且只有⼀个,则获取这个⽤户的信息(测试⽤户名和密码)。如果成功,返回true。若有异常出现(⽐如⽤户存在但是密码错,或者被禁⽤了,返回false。
到这⾥基本部分的验证结束了。功能上,可以验证整个AD中存在并且合法的⽤户。那么,⽤户组的需求怎么实现?⽐如只有⽤户组Team1中的⽤户可以登录?答案是上⾯的Filter。上⾯我们⽤了⼀个最简单的Filter,filter = "sAMAccountName=" + username。⾃然⽽然,可以就可以想到更改这个Filter,让它返回"属于⽤户组Team1并且⽤户名是xxxx"的⽤户。写法很简单,只需要改成final String filter = "(&(CN=" + username + ")(memberof=CN=Team1,CN=Users,DC=dc,DC=testdc,DC=com))";即可。上⾯的filter,有两个条件:1. CN=⽤户名2. memberof=CN=Team1,CN=Users,DC=dc,DC=testdc,DC=com,也就是⽤户是(CN=Team1,CN=Users,DC=dc,DC=testdc,DC=com)的⼀个成员。最前⾯的&表⽰这两个条件是相与的关系。memberof的值是怎么来的呢,可以在LDAP Browser中查看⼀个Team1组中的⽤户,其memberof属性:在测试Filter的时候,可以在LDAP Browser中进⾏测试(右键New Pfofile -- 属性-- Entry页,点击Filter右侧的漏⽃图标,可以在FilterBuilder中测试)最后有个关于⽤户被禁⽤的问题。如果⽤户被禁⽤了,上⾯的代码会抛出⼀个异常,需要捕捉异常。如果不想捕捉异常,可以使⽤(!(userAccountControl:1.2.840.113556.1.4.803:=2))来过滤出所有没有被禁⽤的⽤户。
2023年6月21日发(作者:)
Java实现LDAP认证(上)Baidu脑残,把原来的空间改得不伦不类。所以把⼀些技术的东西挪到这⾥。我找到两种⽅法,⼤同⼩异,第⼀种是通过Spring,适合已经采⽤Spring的项⽬。⼀般来说⽤户名和密码都是保存在数据库中。现在有这个需求,⽤户名和密码是保存在M$的AD中。那么如何进⾏认证。我找到的⽅法有两种,⼀种是jcifs,另⼀种是Spring Security。这⾥⽤了Spring Security,参考了CAS SSO中LDAP验证的实现。⾸先建⼀个测试环境。在Server 2003中安装Domain,这个没啥说的,⽹上⽂章⼀⼤把。安装好以后,创建测试⽤的⽤户组(Team1)和两个⽤户(test,test1)。test⽤户属于组Team1,test1则不属于。如下图。Domain的名称是。创建好以后,在Eclipse中创建Java项⽬,加⼊以下的需要的Jar包:开始写代码。初始化部分,⼀些LDAP的参数设置。static { LdapContextSource cs = new LdapContextSource(); heEnvironmentProperties(false); ("ldap://192.168.1.200:389"); e("CN=Users,DC=dc,DC=testdc,DC=com"); henticationSource(new AuthenticationSource() { public String getCredentials() { return "
换句话说,上⾯的查询,可以这样理解。⾸先,我们通过Search Base的设置(setBase("CN=Users,DC=dc,DC=testdc,DC=com"))设置了查询的范围。然后上⾯的filter = "sAMAccountName=" + username; 设置了查询的条件。例如sAMAccountName=test的意思就是在"CN=Users,DC=dc,DC=testdc,DC=com"所表⽰的这么多对象中,查找sAMAccountName=test的对象。换句话说,就是查找登录的⽤户,在AD服务器中是否存在? 然后程序中继续判断: if (y()) { n("Search for " + filter + " returned 0 results."); return false; } if (() > 1) { n("Search for " + filter + " returned multiple results, which is not allowed."); return false; }不存在,或者多余⼀个存在,都是错误。返回;
for (final String dn : cns) { DirContext test = null; String finalDn = dn; try { n("Performing LDAP bind with credential: " + dn); test = textSource().getContext( finalDn, password); if (test != null) { return true; } } catch (final Exception e) { tackTrace(); } finally { if (test != null) { (); } } }如果存在,并且只有⼀个,则获取这个⽤户的信息(测试⽤户名和密码)。如果成功,返回true。若有异常出现(⽐如⽤户存在但是密码错,或者被禁⽤了,返回false。
到这⾥基本部分的验证结束了。功能上,可以验证整个AD中存在并且合法的⽤户。那么,⽤户组的需求怎么实现?⽐如只有⽤户组Team1中的⽤户可以登录?答案是上⾯的Filter。上⾯我们⽤了⼀个最简单的Filter,filter = "sAMAccountName=" + username。⾃然⽽然,可以就可以想到更改这个Filter,让它返回"属于⽤户组Team1并且⽤户名是xxxx"的⽤户。写法很简单,只需要改成final String filter = "(&(CN=" + username + ")(memberof=CN=Team1,CN=Users,DC=dc,DC=testdc,DC=com))";即可。上⾯的filter,有两个条件:1. CN=⽤户名2. memberof=CN=Team1,CN=Users,DC=dc,DC=testdc,DC=com,也就是⽤户是(CN=Team1,CN=Users,DC=dc,DC=testdc,DC=com)的⼀个成员。最前⾯的&表⽰这两个条件是相与的关系。memberof的值是怎么来的呢,可以在LDAP Browser中查看⼀个Team1组中的⽤户,其memberof属性:在测试Filter的时候,可以在LDAP Browser中进⾏测试(右键New Pfofile -- 属性-- Entry页,点击Filter右侧的漏⽃图标,可以在FilterBuilder中测试)最后有个关于⽤户被禁⽤的问题。如果⽤户被禁⽤了,上⾯的代码会抛出⼀个异常,需要捕捉异常。如果不想捕捉异常,可以使⽤(!(userAccountControl:1.2.840.113556.1.4.803:=2))来过滤出所有没有被禁⽤的⽤户。
发布评论