在前端對用戶提交的密碼進行MD5加密,可以提高頁面在提交數據時的安全性,要實現此功能思路如下:
1.用JavaScript攔截提交
2.通過js對需要加密的信息進行MD5加密
3.繼續進行的提交
相關代碼和說明如下:
md5.js文件點此下載
<html>
<head>
<script src="md5.js"></script>
<script>
function md5passwd(){
passwd = document.getElementById("password").value;
document.getElementById("password").value = hex_md5(passwd);
return true;
}
</script>
</head>
<form action="clock.jsp" method="post" onsubmit="return md5passwd()">
<input type="text" name="username" id="username">
<input type="password" name="password" id="password" value="">
<input type="submit" id="submit" name="submit">
</form>
在表單通過配置屬性οnsubmit="return md5passwd()"來攔截的提交,在md5passwd()函數中,如果return false;,則終止的提交,如果return ture;,則在執行完該函數後繼續提交。
在測試過程中可以使用谷歌瀏覽器開發工具console.log()來進行查看,看是否加密成功。