CMS内容管理系统项目中登陆和注销的小结

一:项目介绍

CMS是"Content Management System"的缩写,意为"内容管理系统",一个管理内容的后台管理系统。

技术选型:
              开发工具:eclipse
              数据库:Mysql
              后端框架:SSM
              前端框架:jQuery、Bootstrap、GridManager表格插件

二:后台管理页面登陆

1.实现思路

                 1.Controller【跳转到login.jsp】

                 2.页面发送登陆请求到controller

                 3.调用service登陆查询

                 4.用户名不存在或者密码错误则跳转到login.jsp

                 5.密码正确登陆成功,将信息放入session

2.代码实现

                 1.controller层

    @ResponseBody
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public AjaxResult login(HttpServletResponse resp, HttpSession session, String username, String password,Integer remember) {
		try {
			Employee emp = service.login(username, password);
			session.setAttribute("Emp_IN_SESSION", emp);
			// 创建cookie
			Cookie c1 = new Cookie("username", username);
			Cookie c2 = new Cookie("password", password);
			if (remember != null) { // 记住
				// 设置路径
				c1.setPath("/");
				c2.setPath("/");
				// 设置生命周期
				c1.setMaxAge(60 * 60 * 24 * 7);
				c2.setMaxAge(60 * 60 * 24 * 7);
				// 添加cookie到浏览器端
				resp.addCookie(c1);
				resp.addCookie(c2);
			} else { // 不记住
				// 设置路径
				c1.setPath("/");
				c2.setPath("/");
				// 重新设置生命周期
				c1.setMaxAge(0);
				c2.setMaxAge(0);
				// 添加到浏览器端
				resp.addCookie(c1);
				resp.addCookie(c2);
			}
			return new AjaxResult();
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResult(false, e.getMessage());
		}
	}

                 2.service层

	@Override
	public Employee login(String username, String password) throws Exception {
		Employee emp = mapper.findByUsername(username);
		if (emp == null) {
			throw new Exception("用户不存在");
		} else {
			if (!password.equals(emp.getPassword())) {
				throw new Exception("密码错误");
			}
		}
		return emp;
	}

二:后台管理页面注销

1.实现思路

                 点击注销发送请求到controller层,直接销毁session

2.代码实现

    @RequestMapping("/logout")
	public String logout(HttpSession session) {
		// 销毁session
		session.invalidate();
		return "login";
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值