@ResponseBody @RequestMapping("/login") public JSONObject login(String name,String password,HttpSession session)throws Exception { JSONObject result = new JSONObject(); User temp = userDao.findByName(name); if(temp==null) { result.put("success", false); result.put("msg","用户名不存在"); }else{ if(temp.getPwd().equals(password)) { result.put("success", true); result.put("msg","登陆成功"); session.setAttribute("currentUser", temp); //把当前用户信息存到session中 }else { result.put("success", false); result.put("msg","密码错误"); } } return result; }
@ResponseBody @RequestMapping("/login") public JSONObject login(String name,String password)throws Exception { JSONObject result = new JSONObject(); //获取 subject Subject subject=SecurityUtils.getSubject(); //封装用户数据 UsernamePasswordToken token=new UsernamePasswordToken(name,CryptographyUtil.md5(password, "java")); //执行登陆 shiro的登陆 try { subject.login(token); result.put("success", true); result.put("msg","登陆成功"); User user = userDao.findByName(name); SecurityUtils.getSubject().getSession().setAttribute("currentUser", user); //把当前用户信息存到session中 } catch (UnknownAccountException e) { result.put("success", false); result.put("msg","用户名不存在"); }catch (IncorrectCredentialsException e) { result.put("success", false); result.put("msg","密码错误"); } return result; }
站长微信:xiaomao0055
站长QQ:14496453