当前位置:   article > 正文

登录注册功能使用md5对密码进行加密_禅道登录自动化脚本md5加密怎么写

禅道登录自动化脚本md5加密怎么写

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

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

  1. package com.train.util;
  2. import java.math.BigInteger;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. public class MD5Utils {
  6. /**
  7. * 使用md5的算法进行加密
  8. */
  9. public static String md5(String plainText) {
  10. byte[] secretBytes = null;
  11. try {
  12. secretBytes = MessageDigest.getInstance("md5").digest(
  13. plainText.getBytes());
  14. } catch (NoSuchAlgorithmException e) {
  15. throw new RuntimeException("没有md5这个算法!");
  16. }
  17. String md5code = new BigInteger(1, secretBytes).toString(16);// // 16进制数字
  18. // 如果生成数字未满32位,需要前面补0
  19. for (int i = 0; i < 32 - md5code.length(); i++) {
  20. md5code = "0" + md5code;
  21. }
  22. return md5code;
  23. }

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

  1. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  2. throws ServletException, IOException {
  3. request.setCharacterEncoding("utf-8");
  4. String userName = request.getParameter("userName");
  5. String passWord = MD5Utils.md5(request.getParameter("passWord"));
  6. userModel model = new userModel();
  7. model.setUserName(userName);
  8. model.setPassWord(passWord);
  9. userService service = new userService();
  10. try {
  11. userModel m = service.selectByName(model.getUserName());
  12. if (m == null) { // 如果找不到同名的记录,则可以注册
  13. int i = service.save(model);
  14. if (i > 0) {
  15. request.setAttribute("msg", "注册成功!请登录!");
  16. request.getRequestDispatcher("login.jsp").forward(request, response);
  17. } else {
  18. request.setAttribute("msg", "注册失败!请重新注册!");
  19. request.getRequestDispatcher("index.jsp").forward(request, response);
  20. }
  21. } else {
  22. request.setAttribute("msg", "用户名重复!请重新注册!");
  23. request.getRequestDispatcher("index.jsp").forward(request, response);
  24. }
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. }

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

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

  1. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  2. throws ServletException, IOException {
  3. request.setCharacterEncoding("utf-8");
  4. String userName = request.getParameter("userName");
  5. String passWord = MD5Utils.md5(request.getParameter("passWord"));
  6. userModel model = new userModel();
  7. model.setUserName(userName);
  8. model.setPassWord(passWord);
  9. userService service = new userService();
  10. if (request.getSession().getAttribute("checkcode_session").equals(request.getParameter("checkCode"))) {
  11. try {
  12. userModel md = service.selectByNameAndPass(model);
  13. if (md != null) {
  14. // 找到记录,登录成功
  15. request.getSession().setAttribute("userName", md.getUserName());
  16. request.getRequestDispatcher("welcome.jsp").forward(request, response);
  17. } else {
  18. request.setAttribute("msg", "登录失败!请重试!");
  19. request.getRequestDispatcher("login.jsp").forward(request, response);
  20. }
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. } else {
  25. request.setAttribute("msg", "验证码输入有误!请重试!");
  26. request.getRequestDispatcher("login.jsp").forward(request, response);
  27. }
  28. }








本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号