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

时间:2022-07-07 04:50:45

开发环境:jdk1.7,eclipse

框架:springmvc,mybatis

工具:maven

以下代码复制即可实现MD5加密

创建一个mave项目,加web。不懂得可以搜索一下就有了。

注册用户的JSP页面代码如下。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<%@ 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,搜一下,我不知道怎么把这个文件传到这上面让你们下载。

JSP登陆页面的代码,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<%@ 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>

接着写后台代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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加密了。其他缺省的代码都很简单,就不都写出来了,看懂逻辑就会做了。

附上数据库中保存的密码是这样的。

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/nihaoa50/article/details/61415945