JAVA简单实现MD5注册登录加密

    xiaoxiao2021-03-25  38

    文章目录

    创建一个mave项目,加web。不懂得可以搜索一下就有了。注册用户的JSP页面代码如下。需要你自己取建一个UserDto的类,我用的是UserDto的属性来传值的。 还要引入jquery MD5,搜一下,我不知道怎么把这个文件传到这上面让你们下载。 http://pan.baidu.com/s/1bp9t8Vd 去网盘下 JSP登陆页面的代码,接着写后台代码 划重点(∩_∩)

    开发环境:jdk1.7,eclipse 框架:springmvc,mybatis 工具:maven 以下代码复制即可实现MD5加密

    创建一个mave项目,加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>

    需要你自己取建一个UserDto的类,我用的是UserDto的属性来传值的。 还要引入jquery MD5,搜一下,我不知道怎么把这个文件传到这上面让你们下载。 http://pan.baidu.com/s/1bp9t8Vd 去网盘下 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>

    接着写后台代码

    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){ //进行加密,页面传过来的不是明文,是一个哈希值,对哈希再加密 String s=userDto.getUser_psw(); String smi=convertMD5(s); userDto.setUser_psw(smi); userDao.insertUser(userDto); return new ModelAndView("NewFile.jsp"); } /* * 验证用户名 */ @RequestMapping("/authUser") public ModelAndView authUser(UserDto userDto){ int i=0; //对用户登录传过来的哈希密码先进行加密 String s=userDto.getUser_psw(); String smi=convertMD5(s); //加密后,与数据库存储的密码进行比对 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"); } /** * 加密解密算法 执行一次加密,两次解密 */ 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; } }

    这样就做了一个简单的MD5加密了。其他缺省的代码都很简单,就不都写出来了,看懂逻辑就会做了。 附上数据库中保存的密码是这样的。 不足之处还请指点。



    划重点(∩_∩)

    本人程序媛一枚,因为离港澳较近,周末兼职港澳人肉代购。

    欢迎各位大佬添加本人微信,还会经常有点赞活动送价值不菲的小礼品哦。

    即使现在不需要代购,等以后有了女(男)朋友、有了宝宝就肯定会需要的喽。

    动动手指头,扫码一下,就当是对本博文的支持嘛。 微 信 号:YS334466888

    转载请注明原文地址: https://ju.6miu.com/read-27451.html

    最新回复(0)