用户名+密码登录ldap服务器,注意密码没有存储在ldap中的password
2017-03-16 09:45
316 查看
用用户名+密码模拟登录ldap服务器,不是从ldap中获取密码比较
public class LdapUserAuthenticate {
private String URL = "ldap://10.41.83.236:389/";
private String BASEDN = "dc=zte,dc=intra";
private String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
private LdapContext ctx = null;
private Hashtable<String, String> env=null;
private Control[] connCtls = null;
private SearchResult searchResult;
private void connectToLDAPServer() {
env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
env.put(Context.PROVIDER_URL, URL + BASEDN);// LDAP server
env.put(Context.SECURITY_AUTHENTICATION, "simple");
// 此处若不指定用户名和密码,则自动转换为匿名登录
env.put(Context.SECURITY_PRINCIPAL,
"cn=gitlab,ou=NM,ou=Central R&D Institute,ou=R&D Institute,dc=zte,dc=intra");
env.put(Context.SECURITY_CREDENTIALS, "gitlab");
try {
connCtls = new Control[] { new LdapADManagerControl() };
ctx = new InitialLdapContext(env, connCtls);
} catch (javax.naming.AuthenticationException e) {
System.out.println("Authentication faild: " + e.toString());
} catch (Exception e) {
System.out.println("Something wrong while authenticating: " + e.toString());
}
}
class LdapADManagerControl implements Control {
@Override
public String getID() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isCritical() {
// TODO Auto-generated method stub
return false;
}
@Override
public byte[] getEncodedValue() {
// TODO Auto-generated method stub
return null;
}
}
private String getUserDN(String ID) {
String userDN = "";
connectToLDAPServer();
try {
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<?> en = ctx.search("", "sAMAccountName=" + ID, constraints);
if (en == null) {
System.out.println("Have no NamingEnumeration.");
}
if (!en.hasMoreElements()) {
System.out.println("Have no element.");
}
while (en != null && en.hasMoreElements()) {// maybe more than one // // element
Object obj = en.nextElement();
if (obj instanceof SearchResult) {
SearchResult si = (SearchResult) obj;
userDN += si.getName();
userDN += "," + BASEDN;
searchResult = si;
} else {
System.out.println(obj);
}
}
} catch (Exception e) {
System.out.println("Exception in search():" + e);
}
return userDN;
}
public String authenricate(String ID, String password) {
String username = null;
if (ID.equals("") || password.equals(""))
return null;
else {
String userDN = "";
try {
userDN = getUserDN(ID);
if (userDN.equals(""))
return null;
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, userDN);
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
ctx.reconnect(connCtls);
String longName = (String)searchResult.getAttributes().get("CN").get();
username = longName.split("\\d+")[0];
return username;
} catch (AuthenticationException e) {
System.out.println(userDN + " is not authenticated");
System.out.println(e.toString());
} catch (NamingException e) {
System.out.println(userDN + " is not authenticated");
}catch (Exception e) {
System.out.println(userDN + " is not authenticated");
}
return null;
}
}
}
LdapUserAuthenticate authen = new LdapUserAuthenticate();
username = authen.authenricate(userid,password);
if (username == null) {
System.out.println("登陆失败");
response.sendRedirect("login.html");
}else{
System.out.println("登陆成功");
CookieUtil.addCookie(response, "userName", username, 1800);
response.sendRedirect(referer);
}
public class LdapUserAuthenticate {
private String URL = "ldap://10.41.83.236:389/";
private String BASEDN = "dc=zte,dc=intra";
private String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
private LdapContext ctx = null;
private Hashtable<String, String> env=null;
private Control[] connCtls = null;
private SearchResult searchResult;
private void connectToLDAPServer() {
env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
env.put(Context.PROVIDER_URL, URL + BASEDN);// LDAP server
env.put(Context.SECURITY_AUTHENTICATION, "simple");
// 此处若不指定用户名和密码,则自动转换为匿名登录
env.put(Context.SECURITY_PRINCIPAL,
"cn=gitlab,ou=NM,ou=Central R&D Institute,ou=R&D Institute,dc=zte,dc=intra");
env.put(Context.SECURITY_CREDENTIALS, "gitlab");
try {
connCtls = new Control[] { new LdapADManagerControl() };
ctx = new InitialLdapContext(env, connCtls);
} catch (javax.naming.AuthenticationException e) {
System.out.println("Authentication faild: " + e.toString());
} catch (Exception e) {
System.out.println("Something wrong while authenticating: " + e.toString());
}
}
class LdapADManagerControl implements Control {
@Override
public String getID() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean isCritical() {
// TODO Auto-generated method stub
return false;
}
@Override
public byte[] getEncodedValue() {
// TODO Auto-generated method stub
return null;
}
}
private String getUserDN(String ID) {
String userDN = "";
connectToLDAPServer();
try {
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<?> en = ctx.search("", "sAMAccountName=" + ID, constraints);
if (en == null) {
System.out.println("Have no NamingEnumeration.");
}
if (!en.hasMoreElements()) {
System.out.println("Have no element.");
}
while (en != null && en.hasMoreElements()) {// maybe more than one // // element
Object obj = en.nextElement();
if (obj instanceof SearchResult) {
SearchResult si = (SearchResult) obj;
userDN += si.getName();
userDN += "," + BASEDN;
searchResult = si;
} else {
System.out.println(obj);
}
}
} catch (Exception e) {
System.out.println("Exception in search():" + e);
}
return userDN;
}
public String authenricate(String ID, String password) {
String username = null;
if (ID.equals("") || password.equals(""))
return null;
else {
String userDN = "";
try {
userDN = getUserDN(ID);
if (userDN.equals(""))
return null;
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, userDN);
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
ctx.reconnect(connCtls);
String longName = (String)searchResult.getAttributes().get("CN").get();
username = longName.split("\\d+")[0];
return username;
} catch (AuthenticationException e) {
System.out.println(userDN + " is not authenticated");
System.out.println(e.toString());
} catch (NamingException e) {
System.out.println(userDN + " is not authenticated");
}catch (Exception e) {
System.out.println(userDN + " is not authenticated");
}
return null;
}
}
}
LdapUserAuthenticate authen = new LdapUserAuthenticate();
username = authen.authenricate(userid,password);
if (username == null) {
System.out.println("登陆失败");
response.sendRedirect("login.html");
}else{
System.out.println("登陆成功");
CookieUtil.addCookie(response, "userName", username, 1800);
response.sendRedirect(referer);
}
相关文章推荐
- 服务器登录用户名,密码修改后,WebServcie不能访问,需要输入用户名密码
- svn checkout时连接不到服务器,重装也没有弹出用户名和密码输入框的问题
- 完成系统登录程序,从命令行输入用户名和密码,如果没有输入用户名和密码,则提示输入用户名和密码;如果输入了用户名但是没有输入密码,则提示用户输入密码,然后判断用户名是否是mldn,密码是否是hello,
- Oauth支持的5类 grant_type 及说明 authorization_code — 授权码模式(即先登录获取code,再获取token) password — 密码模式(将用户名,密码传
- 完成系统登录程序,从命令行输入用户名和密码,如果没有输入用户名和密码,则提示输入用户名和密码;如果输入了用户名但是没有输入密码,则提示用户输入密码,然后判断用户名是否是mldn,密码是否是hello,
- 带有远程服务器,登录名和密码的存储过程 查询范例
- 完成系统登录程序,从命令行输入用户名和密码,如果没有输入用户名和密码,则提示输入用户名和密码;如果输入了用户名但是没有输入密码,则提示用户输入密码,然后判断用户名是否是mldn,密码是否是hello,
- hibernate登录过程中怎么传用户名(name)和密码(password)然后查到用户的ID并返回到页面上
- 制作login.jsp页面,要求实现提供用户登陆的界面, 用户通过该界面输入用户名(控件名userName)和密码(控件名password), 然后点击“登录”按钮实现登陆操作.
- 恢复其它sql2000数据库的.bak文件到另一服务器中的数据库后,发现有用户名没有登录名
- 实现用户登录时若没有输入用户名,给出用户名不能为空的提示,若没有输入密码,给出密码不能为空的提示
- Android学习(19) -- 数据存储之File (简单登录保存用户名和密码)
- 完成系统登录程序,从命令行输入用户名和密码,如果没有输入用户名和密码,则提示输入用户名和密码;如果输入了用户名但是没有输入密码,则提示用户输入密码,然后判断用户名是否是mldn,密码是否是hello,
- 有密码和用户名却进不去共享文件(在进去过共享文件后,又修改了密码,导致没有共享文件的登录框)
- 010_04用户登录(用户名,密码交给服务器并验证)
- 登录时,记住用户名和密码的功能——HTML5 WEB存储(localStorage)
- SSH 免用户名和密码 及登录多台服务器
- 完成系统登录程序,从命令行输入用户名和密码,如果没有输入用户名和密码,则提示输入用户名和密码;如果输入了用户名但是没有输入密码,则提示用户输入密码,然后判断用户名是否是mldn,密码是否是hello,
- 完成系统登录程序,从命令行输入用户名和密码,如果没有输入用户名和密码,则提示输入用户名和密码;如果输入了用户名但是没有输入密码,则提示用户输入密码,然后判断用户名是否是mldn,密码是否是hello,
- 完成系统登录程序,从命令行输入用户名和密码,如果没有输入用户名和密码,则提示输入用户名和密码;如果输入了用户名但是没有输入密码,则提示用户输入密码,然后判断用户名是否是mldn,密码是否是hello,