版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。
@RequestMapping("/login") public Resultlogin(String username, String password, HttpServletResponse response, HttpServletRequest request) {	//1.从数据库中查询用户名对应的用户的对象	User user = userService.findById(username);	if (user == null) {	//2.判断用户是否为空 为空返回数据	return new Result(false, StatusCode.LOGINERROR, "用户名或密码错误");	}	//3如果不为空格 判断 密码是否正确 正确则登录成功	if(BCrypt.checkpw(password,user.getPassword())){	//成功	Mapinfo = new HashMap();	info.put("role","USER");	info.put("success","SUCCESS");	info.put("username",username);	//1.生成令牌	String jwt = JwtUtil.createJWT(UUID.randomUUID().toString(), JSON.toJSONString(info), null);	//2.设置cookie中	Cookie cookie = new Cookie("Authorization",jwt);	response.addCookie(cookie);	//3.设置头文件中	response.setHeader("Authorization",jwt);	return new Result(true, StatusCode.OK, "成功",jwt);	}else{	//失败	return new Result(false, StatusCode.LOGINERROR, "用户名或密码错误");	} }