登录注册功能使用md5对密码进行加密

    之前一直搞不清楚md5加密,从网上查了好多资料、看了几个视频都没实现成功。这一次参加学校的集中实习,培训老师带我们简单实现了一下。

    要简单实现并不难,先从网上down一个简单的md5算法:

package com.train.util;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
	/**
	 * 使用md5的算法进行加密
	 */
	public static String md5(String plainText) {
		byte[] secretBytes = null;
		try {
			secretBytes = MessageDigest.getInstance("md5").digest(
					plainText.getBytes());
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException("没有md5这个算法!");
		}
		String md5code = new BigInteger(1, secretBytes).toString(16);// // 16进制数字
		// 如果生成数字未满32位,需要前面补0
		for (int i = 0; i < 32 - md5code.length(); i++) {
			md5code = "0" + md5code;
		}
		return md5code;
	}
}

然后在 Servlet或者控制器里直接调用MD5的这个静态方法就可以了:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		String userName = request.getParameter("userName");
		String passWord = MD5Utils.md5(request.getParameter("passWord"));
		userModel model = new userModel();
		model.setUserName(userName);
		model.setPassWord(passWord);
		userService service = new userService();
		try {
			userModel m = service.selectByName(model.getUserName());
			if (m == null) { // 如果找不到同名的记录,则可以注册
				int i = service.save(model);
				if (i > 0) {
					request.setAttribute("msg", "注册成功!请登录!");
					request.getRequestDispatcher("login.jsp").forward(request, response);
				} else {
					request.setAttribute("msg", "注册失败!请重新注册!");
					request.getRequestDispatcher("index.jsp").forward(request, response);
				}
			} else {
				request.setAttribute("msg", "用户名重复!请重新注册!");
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

这样注册进去的就是经过md5加密后的密文了。

然后登录功能要比对密码的时候,从数据库中取出的是密文,需要将用户输入的密码再进行md5加密以后再比对,否则就会失败:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		String userName = request.getParameter("userName");
		String passWord = MD5Utils.md5(request.getParameter("passWord"));
		userModel model = new userModel();
		model.setUserName(userName);
		model.setPassWord(passWord);

		userService service = new userService();

		if (request.getSession().getAttribute("checkcode_session").equals(request.getParameter("checkCode"))) {

			try {
				userModel md = service.selectByNameAndPass(model);
				if (md != null) {
					// 找到记录,登录成功
					request.getSession().setAttribute("userName", md.getUserName());
					request.getRequestDispatcher("welcome.jsp").forward(request, response);
				} else {
					request.setAttribute("msg", "登录失败!请重试!");
					request.getRequestDispatcher("login.jsp").forward(request, response);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			request.setAttribute("msg", "验证码输入有误!请重试!");
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}

	}








你可能感兴趣的:(Web)