在java程序中,使用sAMAccountName作为登录名通过LDAP目录库验证

近来,要做一套对项目开发、跟踪、管理、多服务器同步备份的系统集成。主要结合svn、apache、tomcat、bugzilla、sendmail、openSSL、LDAP这些开源优秀软件在ubuntu下实现。其中涉及到JAVA EE的WEB开发,EMAIL、NDS应用模块的配置和结合,数据加密,项目管理过程设计,SVN数据的备份与恢复等等。而我和几个teammates主要负责开发一个web应用程序,对svn中各个库的用户权限进行详细管理。

 

Linux下这些软件的结合是由一位Linux高手用了两个月时间,一步一步的配置起来,间中遇到的各种各样问题,在大家的努力下,终于把整个系统搭建起来。

 

因为公司原来就已经组建了一个非常完善的LDAP目录库,LDAP目录库,就像一个通信录,里面已经存放了所以公司人员的基本信息(如姓名,邮箱,职位等等)。这里有一个前提:所有的公司员工作为用户都可以登录这个web应用程序,登录后,系统则再根据那些SVN库对于这个用户是否有开放访问权限,如果有,则展现给用户。所以我们可以充分利用这个LDAP目录库,非常方便的管理这个WEB应用程序的使用用户。

 

现在不谈整个验证过程,就谈谈登录时,如何匹配LDAP目录库的信息,从而通过登录验证。匹配LDAP目录库记录时,要求要提供以下信息:LDAP目录库地址,基准DN,个人的CN,登录密码。如下面的一个例子:

LDAP目录库地址ldap://10.67.10.2:3268/
基准DN: DC=corp,DC=sb
个人的CN: CN=Xiaopeng Deng,OU=HR,DC=CN,DC=corp,DC=sb
登录密码: 123456

 

sAMAccountName是个人的CN结点中的一个属性,例如上述的个人的CN的sAMAccountName的值为:xdeng。我命名它为shortname,即短名。在外国非常流行于使用shortname作为个人在公司中的称号,包括用于各种系统的登录。现在这个web应用程序也要使用这个sAMAccountName作为登录名登录。查了JAVA操作LDAP库的包,解决方法还是有的:

1,用户提供了s

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ADBulkAdmin工具主要用于批量管理AD帐户,可用于批量查看用户常用属性,批量新建用户和设置用户属性等,可以连接指定的域控,可将所有操作日志保存到Access数据。 软件运行要求: 1. 需要安装.NetFramework 4.0以上版本。 2. 需要安装Office 2007以上版本软件。 3. 请不要更改文件名称,内有说明文档。 可进行如下批量操作: 1. 批量查看帐户信息:Check User。输入帐户后可查询所有帐户的常用属性信息,如DisplayName, Name, SamAccountName, EmailAddress, LyncAddress, Company, Department, DN等。 2. 批量新建帐户:New User。先在AD创建一个tempuserou,保证ADBulkAdmin.exe与users.xlsx在同一目录,目前只支持Office 2007及以上版本,在users.xlsx的newuser Sheet输入帐户的相关属性信息,然后点击Run运行即可,新建的帐户会创建到tempuserou,初始密码为abcD.1234,用户下次登录时须修改密码。 3. 批量重置密码:Reset Password。可进行批量重置密码,默认将密码重置为与帐户相同,全为小写,也可自定义密码进行重置,重置后要求用户在下次登录时修改密码。 4. 批量启用/禁用帐户:Xable User。可进行批量启用帐户或禁用帐户。 5. 批量删除帐户:Remove User。可进行批量删除帐户。 6. 批量设置帐户属性:Set Properties。可批量设置帐户属性,只要先在users.xlsx的setprop Sheet输入帐户的属性信息然后点击Run运行即可。 7. 批量查询组信息:Check Group。可批量查询组的属性及类型等信息。 8. 批量添加帐户到组:Add to Group。在文本框内输入帐户samAccountName和要加入的组,然后点击Run运行即可。 9. 批量从组删除帐户:Remove from Group。在文本框输入帐户samAccountName和要从删除的组,然后点击Run运行却可。 10. 可以连接指定域控,而无论本机是否加入域。 11. 会记录所有操作日志并保存在Access数据,可以方便的进行搜索和查询。 如果您使用的是32位Office,请运行ADBulkAdmin-v1.1.0.4 for 32bit Office程序;如果您使用的是64位Office,请运行ADBulkAdmin-v1.1.0.4 for 64bit Office程序
好的,以下是使用Spring LDAP进行账号禁用的程序: 1. 首先需要在pom.xml添加Spring LDAP和Spring Security的依赖: ```xml <dependencies> <!-- Spring LDAP --> <dependency> <groupId>org.springframework.ldap</groupId> <artifactId>spring-ldap-core</artifactId> <version>2.3.3.RELEASE</version> </dependency> <!-- Spring Security --> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-ldap</artifactId> <version>5.5.0</version> </dependency> </dependencies> ``` 2. 配置LDAP连接信息 ```java @Configuration public class LdapConfig { @Value("${ldap.url}") private String url; @Value("${ldap.base}") private String base; @Value("${ldap.username}") private String username; @Value("${ldap.password}") private String password; @Bean public LdapContextSource contextSource() { LdapContextSource contextSource = new LdapContextSource(); contextSource.setUrl(url); contextSource.setBase(base); contextSource.setUserDn(username); contextSource.setPassword(password); return contextSource; } } ``` 3. 编写LdapAccountService类 ```java @Service public class LdapAccountService { private final LdapTemplate ldapTemplate; public LdapAccountService(LdapTemplate ldapTemplate) { this.ldapTemplate = ldapTemplate; } /** * 禁用LDAP账号 * * @param username 用户名 * @return 禁用结果 */ public boolean disableAccount(String username) { // 查询用户Dn String userDn = getUserDn(username); if (StringUtils.isEmpty(userDn)) { return false; } // 设置用户状态为禁用 ModificationItem[] modificationItems = new ModificationItem[]{ new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userAccountControl", "514")) }; try { ldapTemplate.modifyAttributes(userDn, modificationItems); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 查询用户Dn * * @param username 用户名 * @return 用户Dn */ public String getUserDn(String username) { String filter = "(&(objectClass=user)(sAMAccountName=" + username + "))"; SearchControls searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); List<String> result = ldapTemplate.search("", filter, searchControls, (Attributes attributes) -> attributes.get("distinguishedName").get().toString() ); return result.isEmpty() ? null : result.get(0); } } ``` 4. 在Controller调用LdapAccountService进行账号禁用 ```java @RestController public class AccountController { private final LdapAccountService ldapAccountService; public AccountController(LdapAccountService ldapAccountService) { this.ldapAccountService = ldapAccountService; } @PostMapping("/disableAccount") public ResponseEntity<String> disableAccount(@RequestParam String username) { boolean result = ldapAccountService.disableAccount(username); return result ? ResponseEntity.ok("账号禁用成功") : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("账号禁用失败"); } } ``` 以上就是使用Spring LDAP进行账号禁用的程序,希望能够帮到你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值