LDAPAuthenticationServiceImpl implements LDAPAuthenticationService { @Override public User authenticate(String userName, String password) throws Exception { User user = null; LdapContext ctx = null; Hashtable<String, String> env = null; String fullName = null; try { fullName = getFullName(userName); } catch (NamingException e1) { logger.error(userName+" doesn't exist.", e1); } if(fullName != null) { env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY,ldapFactory); env.put(Context.PROVIDER_URL, ldapUrl); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, fullName); env.put(Context.SECURITY_CREDENTIALS, password); try{ ctx = new InitialLdapContext(env,connCtls); }catch(javax.naming.AuthenticationException e){ logger.info("Authentication faild: "+e.toString()); throw e; }catch(Exception e){ logger.error("Something wrong while authenticating: "+e.toString()); throw e; } if(ctx != null) { user = getUserInfo(fullName, ctx, userName); } } return user; }  @Override User authenticate(String userName, String password); @SuppressWarnings("rawtypes") User getUserInfo(String cn, LdapContext ctx, String userName); void setLdapUrl(String ldapUrl); void setLdapBaseDN(String ldapBaseDN); void setLdapFactory(String ldapFactory); void setSolidDN(String solidDN); void setSolidUsername(String solidUsername); void setSolidPwd(String solidPwd);  }
@Test public void testAuthenticate() throws Exception { LDAPAuthenticationServiceImpl ldapAuthenticationServiceImpl = new LDAPAuthenticationServiceImpl(); ldapAuthenticationServiceImpl.setLdapBaseDN("OU=SHA,OU=Normal,OU=1_UserAccount,DC=dianpingoa,DC=com"); ldapAuthenticationServiceImpl.setLdapFactory("com.sun.jndi.ldap.LdapCtxFactory"); ldapAuthenticationServiceImpl.setLdapUrl("ldap: ldapAuthenticationServiceImpl.setSolidDN("cn=Users,DC=dianpingoa,DC=com"); ldapAuthenticationServiceImpl.setSolidUsername("lionauth"); ldapAuthenticationServiceImpl.setSolidPwd("bxHxXopGJOy78Jze3LWi"); User user = ldapAuthenticationServiceImpl.authenticate("xx", "xx"); assertTrue(user==null); user = ldapAuthenticationServiceImpl.authenticate("chen.hua", "xxxxxxxx"); assertTrue(user != null && "陈华.sh".equals(user.getName())); }