SpringBoot實現(xiàn)Mysql使用MD5進行密碼加密的示例
項目開發(fā)中為了保護用戶隱私安全,一般都會用MD5進行密碼加密
以下就簡單舉例SpringBoot 實現(xiàn)Mysql使用MD5進行密碼加密做一個簡單的例子
看下數(shù)據(jù)庫,這邊簡單做了用戶表進行測試

pom.xml添加依賴引用
<!--MD5加密 對注冊的密碼進行加密操作--><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加密類,進行密碼加密
package com.wyh.unit;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/** * @date 2021/4/20 23:34 */public class Md5Utils { /* * md5算法進行密碼加密 * */ public static String code(String str){try{ //1.獲取MessageDigest對象 生成一個MD5加密計算摘要 MessageDigest md = MessageDigest.getInstance('MD5') ; /* str.getBytes() * 使用平臺的默認字符集將此 String 編碼為 byte 序列,并將結(jié)果存儲到一個新的 byte 數(shù)組中. 此方法多用在字節(jié)流中,用與將字符串轉(zhuǎn)換為字節(jié)。 * */ // 計算md5函數(shù) 使用指定的字節(jié)數(shù)組更新摘要md md.update(str.getBytes()); /* * digest()最后確定返回md5 hash值,返回值為8的字符串。 * 因為md5 hash值是16位的hex值,實際上就是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值,用字符串來表示;得到字符串形式的hash值buf.append(Integer.toHexString(i)) ; } return buf.toString() ;}catch (NoSuchAlgorithmException e){ e.printStackTrace(); return null ;} }}
添加頁面
<%-- 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>賬號<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方法對密碼進行加密處理String password= Md5Utils.code(user.getPassword()) ;user.setPassword(password);int i = userService.addUser(user);if(i>0){ return 'redirect:/showAllUser';} return ''; }
進行添加測試,這邊我輸入的密碼是123456

查看數(shù)據(jù)庫

進行解密可以去:https://www.cmd5.com/復制數(shù)據(jù)庫的加密密碼即可

到此這篇關(guān)于SpringBoot實現(xiàn)Mysql使用MD5進行密碼加密的示例的文章就介紹到這了,更多相關(guān)SpringBoot MD5密碼加密內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Python安裝并操作redis實現(xiàn)流程詳解2. 網(wǎng)頁中img圖片使用css實現(xiàn)等比例自動縮放不變形(代碼已測試)3. 部署vue+Springboot前后端分離項目的步驟實現(xiàn)4. Python常用擴展插件使用教程解析5. idea設(shè)置自動導入依賴的方法步驟6. AspNetCore&MassTransit Courier實現(xiàn)分布式事務(wù)的詳細過程7. vue組件庫的在線主題編輯器的實現(xiàn)思路8. ajax post下載flask文件流以及中文文件名問題9. AJAX實現(xiàn)數(shù)據(jù)的增刪改查操作詳解【java后臺】10. PHP字符串前后字符或空格刪除方法介紹

網(wǎng)公網(wǎng)安備