SpringBoot實(shí)現(xiàn)Mysql使用MD5進(jìn)行密碼加密的示例
項(xiàng)目開(kāi)發(fā)中為了保護(hù)用戶隱私安全,一般都會(huì)用MD5進(jìn)行密碼加密
以下就簡(jiǎn)單舉例SpringBoot 實(shí)現(xiàn)Mysql使用MD5進(jìn)行密碼加密做一個(gè)簡(jiǎn)單的例子
看下數(shù)據(jù)庫(kù),這邊簡(jiǎn)單做了用戶表進(jìn)行測(cè)試
pom.xml添加依賴引用
<!--MD5加密 對(duì)注冊(cè)的密碼進(jìn)行加密操作--><dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId></dependency><dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.8.1</version></dependency>
創(chuàng)建unit包創(chuàng)建md5加密類,進(jìn)行密碼加密
package com.wyh.unit;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/** * @date 2021/4/20 23:34 */public class Md5Utils { /* * md5算法進(jìn)行密碼加密 * */ public static String code(String str){try{ //1.獲取MessageDigest對(duì)象 生成一個(gè)MD5加密計(jì)算摘要 MessageDigest md = MessageDigest.getInstance('MD5') ; /* str.getBytes() * 使用平臺(tái)的默認(rèn)字符集將此 String 編碼為 byte 序列,并將結(jié)果存儲(chǔ)到一個(gè)新的 byte 數(shù)組中. 此方法多用在字節(jié)流中,用與將字符串轉(zhuǎn)換為字節(jié)。 * */ // 計(jì)算md5函數(shù) 使用指定的字節(jié)數(shù)組更新摘要md md.update(str.getBytes()); /* * digest()最后確定返回md5 hash值,返回值為8的字符串。 * 因?yàn)閙d5 hash值是16位的hex值,實(shí)際上就是8位的 * */ byte[] byteDigest = md.digest() ; int i ; StringBuffer buf = new StringBuffer('') ; //遍歷byteDigest //加密邏輯,可以debug自行了解一下加密邏輯 for(int offset = 0 ; offset<byteDigest.length ; offset++){i = byteDigest[offset] ;if(i < 0) i += 256 ;if(i < 16) buf.append('0') ;// BigInteger函數(shù)則將8位的字符串轉(zhuǎn)換成16位hex值,用字符串來(lái)表示;得到字符串形式的hash值buf.append(Integer.toHexString(i)) ; } return buf.toString() ;}catch (NoSuchAlgorithmException e){ e.printStackTrace(); return null ;} }}
添加頁(yè)面
<%-- Date: 2021/4/21 Time: 0:04--%><%@ page contentType='text/html;charset=UTF-8' language='java' %><html><head> <title>添加用戶</title></head><body> <form action='/addUser' method='post'>用戶名<input type='text' name='username'/><br>賬號(hào)<input type='text' name='account'/><br>密碼<input type='password' name='password'/><br><input type='submit' value='提交'> </form></body></html>
Controller添加方法
@RequestMapping('/addUser') public String addUser(User user){//MD5方法對(duì)密碼進(jìn)行加密處理String password= Md5Utils.code(user.getPassword()) ;user.setPassword(password);int i = userService.addUser(user);if(i>0){ return 'redirect:/showAllUser';} return ''; }
進(jìn)行添加測(cè)試,這邊我輸入的密碼是123456
查看數(shù)據(jù)庫(kù)
進(jìn)行解密可以去:https://www.cmd5.com/復(fù)制數(shù)據(jù)庫(kù)的加密密碼即可
到此這篇關(guān)于SpringBoot實(shí)現(xiàn)Mysql使用MD5進(jìn)行密碼加密的示例的文章就介紹到這了,更多相關(guān)SpringBoot MD5密碼加密內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼2. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解3. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法4. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法5. 淺談python出錯(cuò)時(shí)traceback的解讀6. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解7. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向8. Nginx+php配置文件及原理解析9. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析10. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無(wú)效問(wèn)題
