1、用戶認證html
public String execute(){ Hashtable env = new Hashtable(); String LDAP_URL = "ldap://8.8.8.8:389"; // LDAP訪問地址 env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, LDAP_URL); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, username); env.put(Context.SECURITY_CREDENTIALS, password); try { dc = new InitialDirContext(env);// 初始化上下文 // 域節點 String searchBase = "DC=abc,DC=com"; String searchFilter = "cn="+username; SearchControls searchCtls = new SearchControls(); // Create the searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); // Specify //設置查詢的屬性,根據登錄用戶姓名獲取ou String returnedAtts[] = {"distinguishedName"};// 定製返回屬性 searchCtls.setReturningAttributes(returnedAtts); // 設置返回屬性集 // 根據設置的域節點、過濾器類和搜索控制器搜索LDAP獲得結果 NamingEnumeration<SearchResult> entries = dc.search(searchBase, searchFilter,searchCtls); SearchResult entry = entries.next(); Attributes attrs = entry.getAttributes(); String disName = attrs.get("distinguishedname").toString(); String[] disNameArray = disName.split(":"); String distinguishedname = disNameArray[1].trim(); //設置session session.put("distinguishedname", distinguishedname); //認證成功,返回success System.out.println("認證成功");//這裏能夠改爲異常拋出。 return "success"; } catch (javax.naming.AuthenticationException e) { System.out.println("認證失敗"); return "fail"; } catch (Exception e) { System.out.println("認證出錯:" + e); return "fail"; } }
2、修改用戶密碼java
用戶認證是相對比較簡單的,簡單幾行代碼便可,微軟是不容許直接經過java修改密碼(C#除外),咱們就須要用到證書。ajax
一、在AD域服務器上,添加角色 證書服務器,點這裏。json
二、在IE中,Internate選項->內容->證書->找到本身的證書而後導出。服務器
三、導出的證書,須要導入到一個文件裏,這個文件叫cacerts.這個文件在,還須要一個工具keytools.exe。位置:網絡
cacerts : D:\soft\myeclipse2013\binary\com.sun.java.jdk.win32.x86_64_1.6.0.u43\jre\lib\securitysession
keytools : D:\soft\myeclipse2013\binary\com.sun.java.jdk.win32.x86_64_1.6.0.u43\bineclipse
咱們須要經過keytools工具,把導出的證書導入到cacerts裏,導入的方法是在DOS命令裏輸入:工具
D:\soft\myeclipse2013\binary\com.sun.java.jdk.win32.x86_64_1.6.0.u43\bin\keytool -import -keystore D:\soft\myeclipse2013\binary\com.sun.java.jdk.win32.x86_64_1.6.0.u43\jre\lib\security\cacerts -storepass changeit -keypass changeit -alias ca -file D:\ca\ca.cerui
複製到dos,回車後提示是否導入,輸入「y」便可。這些都作完就該咱們的編碼了:
public void editPwd() throws NamingException { this.setNewPwd(new String(decode(newPwd))); this.setConfirmPwd(new String(decode(confirmPwd))); //ajax請求的一些代碼 HttpServletResponse response=ServletActionContext.getResponse(); response.setContentType("text/html; charset=utf-8"); Map<String, Object> m = new HashMap<String, Object>(); //校驗兩次輸入的密碼是否一致 if(!newPwd.equals(confirmPwd)){ m.put("success", false); m.put("msg", "兩次密碼輸入不一致,請從新輸入。"); JSONArray jsonArray = JSONArray.fromObject(m); try { PrintWriter out = response.getWriter(); out.print(jsonArray.toString()); } catch (IOException e) { e.printStackTrace(); } } //ladp的一些配置 Hashtable env = new Hashtable(); String adminName = "sja\\administrator"; String adminPassword = "p@ssword"; String userName = session.get("distinguishedname").toString().replace("%20", " "); String newPassword = newPwd; String keystore = this.getClass().getResource("").toString().substring(6).replace("%20", " ").replace("/", "\\")+"cacerts"; System.setProperty("javax.net.ssl.trustStore",keystore); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.SECURITY_AUTHENTICATION,"simple"); env.put(Context.SECURITY_PRINCIPAL,adminName); env.put(Context.SECURITY_CREDENTIALS,adminPassword); env.put(Context.SECURITY_PROTOCOL,"ssl"); String ldapURL = "ldap://8.8.8.8:636"; env.put(Context.PROVIDER_URL,ldapURL); try { PrintWriter out = response.getWriter(); //初始化ldapcontext LdapContext ctx = new InitialLdapContext(env,null); ModificationItem[] mods = new ModificationItem[1]; String newQuotedPassword = "\"" + newPassword + "\""; byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE"); mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("unicodePwd", newUnicodePassword)); // 修改密碼 ctx.modifyAttributes(userName, mods); System.out.println("Reset Password for: " + userName); ctx.close(); m.put("success", true); JSONArray jsonArray = JSONArray.fromObject(m); out.print(jsonArray.toString()); } catch (NamingException e) { System.out.println("Problem resetting password: " + e); m.put("success", false); m.put("msg", "密碼不符合要求或網絡鏈接錯誤,請嘗試從新輸入密碼或聯繫管理員。"); JSONArray jsonArray = JSONArray.fromObject(m); PrintWriter out; e.printStackTrace(); try { out = response.getWriter(); out.print(jsonArray.toString()); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }
代碼寫的很是的爛,只是簡單的實現了功能,但願你們多拍磚。
有問題能夠留言。