牛客网中级项目学习笔记-登录注册

登录注册业务的实现

登录注册的业务层实现统一在UserService中,值得注意的是,通过Map集合来返回业务层的处理结果给Controller。
在注册方法中,注册失败会通过Map存储注册失败原因,注册成功会存储注册成功的ticket,并在Controller层将ticket存入cookie,ticket用于存储用户登录信息。
通过校验时,为了保证用户密码安全性,随机生成salt值与password相加并用MD5加密存入数据库。
在UserService中:
注册代码:

public Map<String,Object> register(String username,String password) {
		Map<String,Object> map=new HashMap<String,Object>();
		if(StringUtils.isBlank(username)) {
			map.put("msg", "用户名不能为空!");
			return map;
		}			

		if(StringUtils.isBlank(password)) {
			map.put("msg", "密码不能为空!");
			return map;
		}
			
		User user=userDAO.selectByName(username);
		if(user!=null) {
			map.put("msg", "用户名已被注册!");
			return map;
		}
		user=new User();
		user.setName(username);
		user.setHeadUrl(String.format(
				"http://images.nowcoder.com/head/%dt.png",new Random().nextInt(1000)));
		user.setSalt(UUID.randomUUID().toString().substring(0, 5));
		user.setPassword(ToutiaoUtil.MD5(password+user.getSalt()));
		userDAO.addUser(user);
		//登录
		//传入ticket,也就是登录成功
		String ticket=addLoginTicket(user.getId());
		map.put("ticket", ticket);
		return map;
	}

登录设计思路相同,直接贴代码:

public Map<String,Object> login(String username,String password){
		Map<String,Object> map=new HashMap<String,Object>();
		if(StringUtils.isBlank(username)) {
			map.put("msg", "用户名不能为空!");
			return map;
		}
		if(StringUtils.isBlank(password)) {
			map.put("msg",  "密码不能为空!");
			return map;
		}
		User user=userDAO.selectByName(username);
		if(user==null) {
			map.put("msg", "用户名不存在!");
			return map;
		}
		if(!user.getPassword().equals(ToutiaoUtil.MD5(password+user.getSalt()))) {
			map.put("msg", "密码不正确!");
			return map;
		}
		//传入ticket,也就是登录成功
		String ticket=addLoginTicket(user.getId());
		map.put("ticket", ticket);
		return map;
	}

在LoginController:中:
注册代码:

@RequestMapping(value="/reg/",method= {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String reg(Model model,@RequestParam("username")String username,
			@RequestParam("password")String password,
			@RequestParam(value="rember",defaultValue="0")int rememberme,
			HttpServletResponse response) {
		try {//控制层要做好异常处理
			Map<String,Object> map=userService.register(username, password);
			//自己设置状态码返回前端,0即正常,1即错误
			if(map.containsKey("ticket")) {
				Cookie cookie=new Cookie("ticket", map.get("ticket").toString());
				cookie.setPath("/");
				if(rememberme>0) cookie.setMaxAge(5*24*3600);
				response.addCookie(cookie);
				return ToutiaoUtil.getJSONString(0, "注册成功");
			}
			else return ToutiaoUtil.getJSONString(1, map);
		}catch(Exception e) {
			logger.error("注册异常:",e.getMessage());
			return ToutiaoUtil.getJSONString(1, "注册异常");
		}
	}

登录代码:

@RequestMapping(value="/login/",method= {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String login(Model model,@RequestParam("username")String username,
			@RequestParam("password")String password,
			@RequestParam(value="rember",defaultValue="0")int rememberme,
			HttpServletResponse response) {
		try {
			Map<String, Object> map=userService.login(username, password);	
			if(map.containsKey("ticket")) {
				Cookie cookie=new Cookie("ticket", map.get("ticket").toString());
				if(rememberme>0) cookie.setMaxAge(5*24*3600);
				cookie.setPath("/");
				response.addCookie(cookie);
				return ToutiaoUtil.getJSONString(0, "登录成功");			
			}else return ToutiaoUtil.getJSONString(1, map);
		}catch(Exception e) {
			logger.error("登录异常:"+e.getMessage());
			return ToutiaoUtil.getJSONString(1, "登录异常");
		}		
	}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值