如何使用密码对Ldap中的用户进行身份验证 package com.ecom.data.access.controller; 导入java.util.ArrayList; 导入java.util.Hashtable; 导入java.util.List; 导入javax.naming.Context; 导入javax.naming.NamingEnumeration; 导入javax.naming.directory.Attribute; 导入javax.naming.directory.Attributes; 导入javax.naming.directory.DirContext; 导入javax.naming.directory.InitialDirContext; 导入javax.naming.directory.SearchControls; 导入javax.naming.directory.SearchResult; 导入javax.servlet.http.HttpServletRequest; 导入javax.servlet.http.HttpServletResponse; 导入org.opends.server.admin.client.ldap.LDAPConnection; 导入org.opends.server.types.ResultCode; 导入org.springframework.ldap.AuthenticationException; 导入org.springframework.ldap.NamingException; 导入org.springframework.web.servlet.ModelAndView; 导入org.springframework.web.servlet.mvc.Controller; 导入com.unboundd.ldap.sdk.BindRequest; 导入com.unboundd.ldap.sdk.BindResult; 导入com.unbounded.ldap.sdk.SimpleBindRequest; 公共类LoginController实现控制器 { 公共静态字符串usersContainer=“dc=example,dc=com”; 公共模型和视图HandlerRequest(HttpServletRequest请求,HttpServletResponse响应)引发异常 { 字符串givenName1=request.getParameter(“名称”); 字符串userpassword1=request.getParameter(“密码”); System.out.println(“Ldap wellcome”); 最终SimpleBindRequest bindRequest=新的SimpleBindRequest(给定名称1,用户密码1); 系统输出打印项次(“之前”); System.out.println(bindRequest); Hashtable env=newhashtable(11);//这里我们为JNDI设置了一些连接哈希表 put(Context.INITIAL\u Context\u工厂,“com.sun.jndi.ldap.LdapCtxFactory”); env.put(Context.PROVIDER\u URL,“ldap://localhost:389"); env.put(Context.SECURITY_PRINCIPAL,“cn=目录管理器”); 环境保护(Context.SECURITY_凭证,“admin”); DirContext ctx=null; NamingEnumeration结果=空; 试一试{ ctx=新的初始目录上下文(env); SearchControls=新的SearchControls(); 控件.setSearchScope(SearchControls.SUBTREE_范围); String lookup=“givenName=“+givenName1+”,dc=example,dc=com”; 字符串obj=“(objectclass=*)”; 结果=ctx.search(查找、对象、控件); while(results.hasMore()) { SearchResult SearchResult=(SearchResult)results.next(); Attributes=searchResult.getAttributes(); Attribute attruser=attributes.get(“givenName”); Attribute attrpwd=attributes.get(“uid”); Attribute org=attributes.get(“o”); 字符串cn=(字符串)attruser.get(); 字符串cn1=(字符串)attrpwd.get(); 字符串cn2=(字符串)org.get(); List li=new ArrayList(); 添加(cn); 加上(cn1); 添加(cn2); if(givenName1.equals(cn)和&userpassword1.equals(cn1)) { ModelAndView modelSuccess=新建ModelAndView(“登录页面”); addObject(“msgSuccess”,li); 回归成功; } 其他的 { ModelAndView modelError=新的ModelAndView(“errorPage”); modelError.addObject(“msgError”,“无效的用户名和密码”); 返回模型错误; } } }捕获(可丢弃的e) { }最后 { 如果(结果!=null) { 尝试 { 结果:关闭(); }捕获(例外e) { } } 如果(ctx!=null){ 试一试{ ctx.close(); }捕获(例外e){ } } } 返回null; } }
我正在使用Spring3。我正在LDAP上工作。我需要做的是验证一个用户的用户名和密码是否正确。我的OpenDs中有许多字段。如果我使用的是给定的名称和名字,那么它将进行身份验证,但是如果我使用密码,那么它将不会进行身份验证,因为存储在openDs中的密码是加密的。所以我需要匹配密码。请给我一些建议。我使用的代码如上所述。密码应存储为称为“哈希”的加密摘要,通常位于如何使用密码对Ldap中的用户进行身份验证 package com.ecom.data.access.controller; 导入java.util.ArrayList; 导入java.util.Hashtable; 导入java.util.List; 导入javax.naming.Context; 导入javax.naming.NamingEnumeration; 导入javax.naming.directory.Attribute; 导入javax.naming.directory.Attributes; 导入javax.naming.directory.DirContext; 导入javax.naming.directory.InitialDirContext; 导入javax.naming.directory.SearchControls; 导入javax.naming.directory.SearchResult; 导入javax.servlet.http.HttpServletRequest; 导入javax.servlet.http.HttpServletResponse; 导入org.opends.server.admin.client.ldap.LDAPConnection; 导入org.opends.server.types.ResultCode; 导入org.springframework.ldap.AuthenticationException; 导入org.springframework.ldap.NamingException; 导入org.springframework.web.servlet.ModelAndView; 导入org.springframework.web.servlet.mvc.Controller; 导入com.unboundd.ldap.sdk.BindRequest; 导入com.unboundd.ldap.sdk.BindResult; 导入com.unbounded.ldap.sdk.SimpleBindRequest; 公共类LoginController实现控制器 { 公共静态字符串usersContainer=“dc=example,dc=com”; 公共模型和视图HandlerRequest(HttpServletRequest请求,HttpServletResponse响应)引发异常 { 字符串givenName1=request.getParameter(“名称”); 字符串userpassword1=request.getParameter(“密码”); System.out.println(“Ldap wellcome”); 最终SimpleBindRequest bindRequest=新的SimpleBindRequest(给定名称1,用户密码1); 系统输出打印项次(“之前”); System.out.println(bindRequest); Hashtable env=newhashtable(11);//这里我们为JNDI设置了一些连接哈希表 put(Context.INITIAL\u Context\u工厂,“com.sun.jndi.ldap.LdapCtxFactory”); env.put(Context.PROVIDER\u URL,“ldap://localhost:389"); env.put(Context.SECURITY_PRINCIPAL,“cn=目录管理器”); 环境保护(Context.SECURITY_凭证,“admin”); DirContext ctx=null; NamingEnumeration结果=空; 试一试{ ctx=新的初始目录上下文(env); SearchControls=新的SearchControls(); 控件.setSearchScope(SearchControls.SUBTREE_范围); String lookup=“givenName=“+givenName1+”,dc=example,dc=com”; 字符串obj=“(objectclass=*)”; 结果=ctx.search(查找、对象、控件); while(results.hasMore()) { SearchResult SearchResult=(SearchResult)results.next(); Attributes=searchResult.getAttributes(); Attribute attruser=attributes.get(“givenName”); Attribute attrpwd=attributes.get(“uid”); Attribute org=attributes.get(“o”); 字符串cn=(字符串)attruser.get(); 字符串cn1=(字符串)attrpwd.get(); 字符串cn2=(字符串)org.get(); List li=new ArrayList(); 添加(cn); 加上(cn1); 添加(cn2); if(givenName1.equals(cn)和&userpassword1.equals(cn1)) { ModelAndView modelSuccess=新建ModelAndView(“登录页面”); addObject(“msgSuccess”,li); 回归成功; } 其他的 { ModelAndView modelError=新的ModelAndView(“errorPage”); modelError.addObject(“msgError”,“无效的用户名和密码”); 返回模型错误; } } }捕获(可丢弃的e) { }最后 { 如果(结果!=null) { 尝试 { 结果:关闭(); }捕获(例外e) { } } 如果(ctx!=null){ 试一试{ ctx.close(); }捕获(例外e){ } } } 返回null; } },spring,ldap,opends,Spring,Ldap,Opends,我正在使用Spring3。我正在LDAP上工作。我需要做的是验证一个用户的用户名和密码是否正确。我的OpenDs中有许多字段。如果我使用的是给定的名称和名字,那么它将进行身份验证,但是如果我使用密码,那么它将不会进行身份验证,因为存储在openDs中的密码是加密的。所以我需要匹配密码。请给我一些建议。我使用的代码如上所述。密码应存储为称为“哈希”的加密摘要,通常位于userPassword或authPassword属性中。LDAP客户端通过使用绑定请求更改会话的授权状态来进行身份验证。Simpl
userPassword
或authPassword
属性中。LDAP客户端通过使用绑定请求更改会话的授权状态来进行身份验证。SimpleBindRequest具有将用作会话授权状态的帐户的可分辨名称(主键)和明文密码
SimpleBindRequest应通过安全连接(SSL或使用StartTLS扩展请求升级的非安全连接)传输到服务器。其他方法(如DIGEST-MD5或CRAM-MD5)不安全或不太安全,因为服务器必须能够解密密码,因此密码必须以可逆加密方式存储。其他安全方法包括GSSAPI或外部SASL机制,但安全连接上的SimpleBindRequest最简单
LDAP目录服务器验证提供的密码,如果验证成功,则更改会话的授权状态,然后向客户端返回绑定响应。绑定响应包含一个结果代码-如果该结果代码为零(0),则验证(和
package com.ecom.data.access.controller;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.opends.server.admin.client.ldap.LDAPConnection;
import org.opends.server.types.ResultCode;
import org.springframework.ldap.AuthenticationException;
import org.springframework.ldap.NamingException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.unboundid.ldap.sdk.BindRequest;
import com.unboundid.ldap.sdk.BindResult;
import com.unboundid.ldap.sdk.SimpleBindRequest;
public class LoginController implements Controller
{
public static String usersContainer = "dc=example,dc=com";
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception
{
String givenName1=request.getParameter("name");
String userpassword1=request.getParameter("password");
System.out.println("Ldap wellcome");
final SimpleBindRequest bindRequest=new SimpleBindRequest(givenName1, userpassword1);
System.out.println("before");
System.out.println(bindRequest);
Hashtable<String, Object> env = new Hashtable<String, Object>(11);// Here we set some connection Hashtable for JNDI
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389");
env.put( Context.SECURITY_PRINCIPAL, "cn=Directory Manager" );
env.put( Context.SECURITY_CREDENTIALS, "admin" );
DirContext ctx = null;
NamingEnumeration<?> results = null;
try {
ctx = new InitialDirContext(env);
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String lookup="givenName="+givenName1+",dc=example,dc=com";
String obj = "(objectclass=*)";
results=ctx.search(lookup, obj, controls);
while (results.hasMore())
{
SearchResult searchResult = (SearchResult) results.next();
Attributes attributes = searchResult.getAttributes();
Attribute attruser = attributes.get("givenName");
Attribute attrpwd=attributes.get("uid");
Attribute org=attributes.get("o");
String cn = (String)attruser.get();
String cn1 = (String)attrpwd.get();
String cn2 = (String)org.get();
List<String> li = new ArrayList<String>();
li.add(cn);
li.add(cn1);
li.add(cn2);
if(givenName1.equals(cn) && userpassword1.equals(cn1))
{
ModelAndView modelSuccess=new ModelAndView("loginPage");
modelSuccess.addObject("msgSuccess", li);
return modelSuccess;
}
else
{
ModelAndView modelError=new ModelAndView("errorPage");
modelError.addObject("msgError", "Invalid UserName and Password");
return modelError;
}
}
} catch (Throwable e)
{
} finally
{
if (results != null)
{
try
{
results.close();
} catch (Exception e)
{
}
}
if (ctx != null) {
try {
ctx.close();
} catch (Exception e) {
}
}
}
return null;
}
}