JAVA簡單實(shí)現(xiàn)MD5注冊登錄加密實(shí)例代碼
開發(fā)環(huán)境:jdk1.7,eclipse
框架:springmvc,mybatis
工具:maven
以下代碼復(fù)制即可實(shí)現(xiàn)MD5加密
創(chuàng)建一個(gè)mave項(xiàng)目,加web。不懂得可以搜索一下就有了。
注冊用戶的JSP頁面代碼如下。
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <script type="text/javascript" src="js/jQuery-2.2.0.min.js"></script> <script type="text/javascript" src="md5/jquery.md5.js"></script> <title>Insert title here</title> </head> <body> <form action="insertUser" method="post" id="myForm"> <table> <tr> <td>用戶名:</td> <td> <input type="text" id="userName" name="user_name" > <input type="hidden" id="pwd" name="user_psw"> <div id="userNameInfo"></div> </td> </tr> <tr> <td>密碼:</td> <td><input type="text" id="password" name="password" onblur="mdjia()"></td> </tr> <tr> <td><input type="button" value="生成頁面hash值" ></td> <td><input type="submit" value="添加用戶"></td> </tr> </table> </form> </body> <script type="text/javascript"> function mdjia(){ var password=$("#password").val(); var pwd=$.md5(password); alert(pwd); $("#pwd").val(pwd); } </script> </html>
需要你自己取建一個(gè)UserDto的類,我用的是UserDto的屬性來傳值的。
還要引入jQuery MD5,搜一下,我不知道怎么把這個(gè)文件傳到這上面讓你們下載。
JSP登陸頁面的代碼,
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <script type="text/javascript" src="js/jQuery-2.2.0.min.js"></script> <script type="text/javascript" src="md5/jquery.md5.js"></script> <title>MD5加密</title> </head> <body> <form action="authUser" method="post" id="myForm"> <table> <tr> <td>用戶名:</td> <td> <input type="text" id="userName" name="user_name" > <input type="hidden" id="pwd" name="user_psw"> <div id="userNameInfo"></div> </td> </tr> <tr> <td>密碼:</td> <td><input type="text" id="password" name="password" onblur="mdjia()"></td> </tr> <tr> <td><input type="button" value="生成頁面hash值" ></td> <td><input type="submit" value="用戶登錄"></td> </tr> </table> </form> </body> <script type="text/javascript"> function mdjia(){ var password=$("#password").val(); var pwd=$.md5(password); alert(pwd); $("#pwd").val(pwd); } </script> </html>
接著寫后臺(tái)代碼
package com.test.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.test.dao.UserDao; import com.test.model.UserDto; /** * * @author 半路出家 * */ @Controller public class UserLogin { @Resource UserDao userDao; /* * 添加用戶 */ @RequestMapping("/insertUser") public ModelAndView insertUser(UserDto userDto){ //進(jìn)行加密,頁面?zhèn)鬟^來的不是明文,是一個(gè)哈希值,對哈希再加密 String s=userDto.getUser_psw(); String smi=convertMD5(s); userDto.setUser_psw(smi); userDao.insertUser(userDto); return new ModelAndView("NewFile.jsp"); } /* * 驗(yàn)證用戶名 */ @RequestMapping("/authUser") public ModelAndView authUser(UserDto userDto){ int i=0; //對用戶登錄傳過來的哈希密碼先進(jìn)行加密 String s=userDto.getUser_psw(); String smi=convertMD5(s); //加密后,與數(shù)據(jù)庫存儲(chǔ)的密碼進(jìn)行比對 userDto.setUser_psw(smi); try { i=userDao.login(userDto); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(i==1){ System.out.println("用戶登錄成功"); }else{ System.out.println("用戶登錄失敗"); } return new ModelAndView("NewFile.jsp"); } /** * 加密解密算法 執(zhí)行一次加密,兩次解密 */ public static String convertMD5(String inStr){ char[] a = inStr.toCharArray(); for (int i = 0; i < a.length; i++){ a[i] = (char) (a[i] ^ 't'); } String s = new String(a); return s; } }
這樣就做了一個(gè)簡單的MD5加密了。其他缺省的代碼都很簡單,就不都寫出來了,看懂邏輯就會(huì)做了。
附上數(shù)據(jù)庫中保存的密碼是這樣的。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
談?wù)凧ava中整數(shù)類型(short int long)的存儲(chǔ)方式
在java中的整數(shù)類型有四種,分別是byte short in long,本文重點(diǎn)給大家介紹java中的整數(shù)類型(short int long),由于byte只是一個(gè)字節(jié)0或1,在此就不多說了,對java中的整數(shù)類型感興趣的朋友一起學(xué)習(xí)吧2015-11-11MyBatis如何實(shí)現(xiàn)多表查詢(多對一、一對多)
這篇文章主要給大家介紹了關(guān)于MyBatis如何實(shí)現(xiàn)多表查詢(多對一、一對多)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Spring Bean生命周期之Bean的實(shí)例化詳解
這篇文章主要為大家詳細(xì)介紹了Spring Bean生命周期之Bean的實(shí)例化,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03Java實(shí)用小技能之快速創(chuàng)建List常用幾種方式
java集合可以說無論是面試、刷題還是工作中都是非常常用的,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)用小技能之快速創(chuàng)建List常用的幾種方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12nacos服務(wù)注冊服務(wù)發(fā)現(xiàn)依賴配置詳解
這篇文章主要為大家介紹了nacos服務(wù)注冊服務(wù)發(fā)現(xiàn)依賴配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL)
MyBatis-Plus是MyBatis的增強(qiáng)工具,本文主要介紹了Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL),具有一定的參考價(jià)值,感興趣的可以了解一下2021-07-07