SSM之MD5加盐加密

tech2022-08-07  137

第一步:MD5工具类

public class Md5Util { public static String md5(String text,String key)throws Exception{ //加密后的字符串 String s = DigestUtils.md5DigestAsHex((text + key).getBytes()); System.out.println("MD5加密后的字符串为:encodeStr="+s); return s; } public static boolean verify(String text,String key,String md5)throws Exception{ //根据传入的秘钥进行验证 String md5Text=md5(text, key); return md5Text.equalsIgnoreCase(md5); } public static boolean verify(String pwd,String dpwd){ String s = null; try { s = md5(pwd, Tool.KEY); } catch (Exception e) { e.printStackTrace(); } return s.equals(dpwd); } }

第二步:接口工具类

public interface Tool { String KEY = "www.fengyu100.com"; }

第三步 : 登录类中添加以下代码

//判断是否登录成功 if (Md5Util.verify(wpwd, Tool.KEY, worker.getWpwd())) { session.setAttribute("user", worker); return "redirect:/admin.jsp"; } //登录加密,直接刷新 if (!worker.getWpwd().equals(wpwd)) { session.setAttribute("login_message", "密码错误!"); return "redirect:/login.jsp"; } String md5Pwd = Md5Util.md5(worker.getWpwd(), Tool.KEY); worker.setWpwd(md5Pwd); service.changePwd(worker); System.out.println("xxxxx" + worker.getWpwd()); session.setAttribute("user", worker); session.setAttribute("login_message", "登录成功!"); return "redirect:/admin.jsp";

完整的登录类如下所示

package com.zhiyou100.controller; import com.zhiyou100.entity.Worker; import com.zhiyou100.service.WorkerLoginService; import com.zhiyou100.util.Md5Util; import com.zhiyou100.util.Tool; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; /** * @program: SSM_integration * @description: 登录验证 * @author: 作者 * @create: 2020-08-27 17:43 */ @Controller public class LoginController { @Autowired private WorkerLoginService service; /** * 获取HttpSession对象,由spring注入 */ @Autowired HttpSession session; @RequestMapping(value = "/login.action", produces = "text/json;charset=utf-8") public String getOneByWnameAndWpwd(String wname, String wpwd, HttpServletRequest req) throws Exception { if (wname == null || wname.equals("")) { session.setAttribute("login_message", "用户名不能为空!"); return "redirect:/login.jsp"; } else if (wpwd == null || wpwd.equals("")) { session.setAttribute("login_message", "密码不能为空!"); return "redirect:/login.jsp"; } Worker worker = service.getOneByWname(wname); System.out.println(wname); System.out.println("pwd=" + worker.getWpwd()); //判断是否登录成功 if (Md5Util.verify(wpwd, Tool.KEY, worker.getWpwd())) { session.setAttribute("user", worker); return "redirect:/admin.jsp"; } //登录加密,直接刷新 if (!worker.getWpwd().equals(wpwd)) { session.setAttribute("login_message", "密码错误!"); return "redirect:/login.jsp"; } String md5Pwd = Md5Util.md5(worker.getWpwd(), Tool.KEY); worker.setWpwd(md5Pwd); service.changePwd(worker); System.out.println("xxxxx" + worker.getWpwd()); session.setAttribute("user", worker); session.setAttribute("login_message", "登录成功!"); return "redirect:/admin.jsp"; } @RequestMapping("/exit.action") public String exit(HttpServletRequest req) { req.getSession().invalidate(); return "redirect:/login.action"; } }

第四步 : mapper.xml中

@Select("select * from worker where wname = #{wname}") Worker getOneByWname(@Param("wname") String wname); @Update("update worker set wpwd = #{wpwd} where wname = #{wname}") void changePwd(Worker worker);

这样就完成了,赶紧试一下吧!!!

最新回复(0)