LDAP账号同步和Windows域集成验证

1 篇文章 0 订阅

应用场景

  • 应用系统中的账号信息除了本地创建的之外,还要有LDAP中的,并且随时与LDAP中的最新数据一致;
  • 公司所有人的电脑都在一个域中管理,员工通过域账号和密码登录他的计算机之后,在登录应用系统之后不再需要输入密码,直接进入系统;
  • 如果员工拥有多个不同的域账号和密码,那么他也可以在选择任意一个域账号来登录应用系统(而不仅仅是登录计算机那个域账号和密码);

目标功能

  • 1,LDAP账号同步
    把LDAP中的用户数据同步到本地数据中
  • 2,LDAP用户登录验证
    根据用户提供的用户名和密码验证用户是否为合法的域用户
  • 3,Windows域集成验证
    比如一个信息管理系统,当用户使用域中(域控,ActiveDirectory)的计算机登录信息管理系统的时候,由于该用户在登录计算机的时候已经通过了身份验证,所以不需要再次输入用户名和密码而直接进入信息管理系统。

测试环境准备

服务器IP:192.168.116.128

服务器域信息:

 

Active Directory 状态:

 

功能:LDAP账号同步

首先,需要获取到LdapContext:

	private LdapContext getLdapContext()throws NamingException{
		Hashtable<String,String> hashtable = new Hashtable<String,String>();
		hashtable.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
		hashtable.put(Context.PROVIDER_URL, "ldap://***.***.***.***:389");//服务器地址
		hashtable.put(Context.SECURITY_AUTHENTICATION, "simple");
		hashtable.put(Context.SECURITY_PRINCIPAL, "Administrator@abc.com");//用户名
		hashtable.put(Context.SECURITY_CREDENTIALS, "******");//密码
		return new InitialLdapContext(hashtable,null);
	}


然后进行相关的搜索设置:

		LdapContext  ctx = getLdapContext();
                //设置分页大小
		ctx.setRequestControls(new Control[] { new PagedResultsControl(15, Control.NONCRITICAL) });
		
		SearchControls control = new SearchControls();
		
		//搜索方式
		control.setSearchScope(SearchControls.SUBTREE_SCOPE);//Search the entire subtree rooted at the named object. 
		//control.setSearchScope(SearchControls.ONELEVEL_SCOPE);//Search one level of the named context
		//control.setSearchScope(SearchControls.OBJECT_SCOPE);//Search the named object
		
		//搜索字段
		String returnedAtts[] = { "displayName", "mail", "telephoneNumber","thumbnailPhoto" };//姓名,邮箱,电话,头像
		control.setReturningAttributes(returnedAtts);
		
		//设置ou和filter
		String ou = "ou=users,ou=beijing,dc=abc,dc=com";
		String filter = "(&(objectClass=user)(objectCategory=person)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";

最后,发起搜索请求,解析搜索结果:

NamingEnumeration<SearchResult> results = ctx.search(ou, filter, control);
		while (results != null && results.hasMoreElements()) {
			SearchResult entry = (SearchResult) results.next();
			String empName = getValueFromAttribute(entry.getAttributes().get(returnedAtts[0]));
			String mail = getValueFromAttribute(entry.getAttributes().get(returnedAtts[1]));
			String telephone = getValueFromAttribute(entry.getAttributes().get(returnedAtts[2]));
			byte[] photoBytes = null;
			Attribute att = (Attribute) entry.getAttributes().get("thumbnailPhoto");
			if(att!=null){
				photoBytes = (byte[])(att.get(0));
			}
			System.out.println(empName+"|"+mail+"|"+telephone+"|"+(photoBytes==null ? 0 : photoBytes.length));
		}

功能:LDAP用户登录验证

我的实现方式如下,有更好方法的朋友还请指教:

private boolean validate(String username,String pwd)throws NamingException{
		Hashtable<String,String> hashtable = new Hashtable<String,String>();
		hashtable.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
		hashtable.put(Context.PROVIDER_URL, "ldap://192.168.116.128:389");//服务器地址
		hashtable.put(Context.SECURITY_AUTHENTICATION, "simple");
		hashtable.put(Context.SECURITY_PRINCIPAL, username);//用户名
		hashtable.put(Context.SECURITY_CREDENTIALS, pwd);//密码
		return new InitialLdapContext(hashtable,null)!=null;
	}

 

功能:Windows集成验证登录

这个功能,使用到了工具http://tomcatspnego.codeplex.com/

下载工具后解压,然后:

复制jar包frdoumesspitc7.jar到tomcat的/lib目录下 

复制SSPAuthentification.dll和SSPAuthentificationx64.dll到tomcat的/bin目录下

在应用的web.xml中增加如下配置:

<security-constraint>
      <display-name>Example Security Constraint</display-name>
      <web-resource-collection>
         <web-resource-name>Protected Area</web-resource-name>
	 	 <!-- Define the context-relative URL(s) to be protected -->
         <url-pattern>/auth.do</url-pattern>
	 	 <!-- If you list http methods, only those methods are protected -->
	 	 <http-method>DELETE</http-method>
         <http-method>GET</http-method>
         <http-method>POST</http-method>
	 	 <http-method>PUT</http-method>
      </web-resource-collection>
      <auth-constraint>
         <!-- Anyone with one of the listed roles may access this area 
	 	 <role-name>utilisateurs</role-name>
	 	 <role-name>users</role-name>
	 	 <role-name>everyone</role-name>-->
	 	 <role-name>everyone</role-name>
      </auth-constraint>
    </security-constraint>

    <!-- Default login configuration -->
    <login-config>
      <auth-method>BASIC</auth-method>
      <realm-name>Example Spnego</realm-name>
    </login-config>

 

就这样,tomcatspnego就能使用了。这里使用到了Tomcat的目录保护功能,而tomcatspnego应该是对tomcat的验证功能做了修改,增加了域信息的检查。这里连没有配置域服务器ip地址都没有配置,tomcatspnego具体如何实现域信息监测就不太清楚了。

 

接下来,在我们自己的应用身份验证中需要用到tomcatspnego留给我们的标记:

    @RequestMapping(value = "/auth")
    public String ntlmAuth(HttpServletRequest request,HttpServletResponse response,HttpSession session){
    	Principal princ = request.getUserPrincipal();
    	if (isLogined()) {
                return "redirect:index.do";
        }
    	if(princ!=null){
    		session.setAttribute("princpalNameInSession", princ.getName());
    	}
    	return "redirect:index.do";
    }

    @RequestMapping("/login")
    public ModelAndView login(String loginName,String password,HttpServletResponse response,HttpSession session,HttpServletRequest request) {
    	if(!response.isCommitted()){
    		ModelAndView mav = new ModelAndView();
			mav.setViewName("login");
    		User user = null;
    		Config config = Config.getInstance(true);
    		if(notEmpty(loginName) && notEmpty(password)){//如果用户名和密码都存在,普通登录,或域账号登录
    			user = userService.getEntityByProperty(User.class, "userName", loginName);
    			if(user==null || user.getStatus()!=UserStatus.Active ){
    				mav.addObject("noUserError", "用户名不存在!");
    			}else{
    				if(user.getType()==UserType.Domain && config.isUseLdapValidate()){//域用户
    					if(domainLoginValidate(loginName,password,request,config)){
    						mav.setViewName("redirect:index.do");//域登录成功
    					}
    				}else{
    					if(localLoginValidate(user,password)){
    						mav.setViewName("redirect:index.do");//本地登录成功
    					}else{
    						mav.addObject("passwordError", "密码输入错误!");
    					}
    				}
    			}
    		}else if(domainLoginValidateByNtlm(request)){//存在域Ntlm变量,尝试域登录
    			//则根据域变量获取到用户名
    			String princpalName = getPrincpalUserName(request);
    			user = userService.getEntityByProperty(User.class, "userName", princpalName);
    			
    			if(user!=null){
    				log.info("用户:"+loginName+",通过获取到本机域信息直接登录成功!");
    			}else{
    				mav.addObject("error","已经检测到您为域用户,但是在系统中没有查询到您的用户信息");
    			}
    		}else{
    			//用户名和密码以及域变量都不存在,重定向到登录页面
    			mav.setViewName("redirect:loginUI.do");
    		}
    		//然后获取到user信息,并加载权限信息,设置“已登录”标志
    		if(user!=null){
    			prepareFunctionPoint(session,user);
    		}
    		return mav;
    	}
    	return null;
    }

    @RequestMapping("/index")
    public String index(HttpServletRequest request,HttpServletResponse response,HttpSession session){
    	if(!response.isCommitted()){
			if (isLogined()) {
				return "main";
			} 
			Config config = Config.getInstance(true);
			if(!config.isUseLdapValidate()){
				return "redirect:loginUI.do";
			}
			// 则尝试根据域变量获取到用户信息;
			User user = getUserFromDbByDomainUserName(request);
			if (user != null) {
				prepareFunctionPoint(session, user);
				return "main";
			}
			return "redirect:loginUI.do";
    	}
    	return null;
	}

 

说明:

这里把应用的首页设置为/auth.do,以便于当用户直接访问/的时候直接跳转到/auth.do;

在进入ntlmAuth方法之前,系统已经利用tomcatspnego来尝试域验证;

在auth方法中尝试获取tomcatspnego给我们留下来的变量,并保存起来;

从定向到index.do,index中尝试通过域变量来获取用户信息(获取成功表示域登陆成功);

login方法正常接收用户名和密码,可以进行本地账号验证和域账号验证。

 

 

# 2018-08-15更新

发现还有朋友在关注这个问题,我顺便更新一下。

我写的的这个适用于Tomcat7,发现Tomcat8用不了。那Tomcat8怎么做呢?其实官方文档里有说明:

点到这里面,找到第三方工具,我用过这个工具还不错,你还可以顺便研究研究其他的。

不好意思我买了个关子,其实直接给出下面这个地址就好了。我多记录了一下我怎么找到这个资源的。

https://github.com/Waffle/waffle里有所有你需要的东西。祝你好运!

 

 

 

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值