完整的登录类如下所示
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"; } }这样就完成了,赶紧试一下吧!!!