希望各位可以指正一下,我想在用户登录之后把用户的id和nickName保存在cookie中,然后再用户发送请求到别的控制类的时候获取这个cookie并且把数据传回即将跳转的页面
这是我的登录的控制类代码
这里我有一个疑惑(我把cookie遍历在cookie添加前但是控制台console却是可以获取到数据的)
@RequestMapping("login")
/*需要注意一点login方法中的参数名称要与前端接受的参数的名称一样*/
public String login(User user , Model model , HttpServletRequest request, HttpServletResponse response){
Cookie[] cookies=request.getCookies();
for (Cookie cookie :cookies){
if (cookie.getName().equals("userId")){
System.err.println("有对应的cookie");
System.err.println("cookie的时效: " + cookie.getMaxAge());
System.err.println("cookie的Comment: " + cookie.getComment());
System.err.println("cookie的Domain: " + cookie.getDomain());
System.err.println("cookie的Name: " + cookie.getName());
System.err.println("cookie的Path: " + cookie.getPath());
System.err.println("cookie的Value: " + cookie.getValue());
System.err.println("cookie的Secure: " + cookie.getSecure());
System.err.println("cookie的Version: " + cookie.getVersion());
}
}
System.err.println(user);
if ("".equals(user.getPhone())||null==user.getPhone() ){ /*电话为空直接判断失败*/
//model要在映射之前不然无法返回消息会爆红
model.addAttribute("errorMsg","捏麻麻地手机号输入一下!");
return "pages/front.login/loginPage"; /*直接返回登录页面再次登录*/
}
boolean loginResult = userService.login(user);
if (loginResult){ //登录成功返回后台首页
if (userService.selectUserByPhone(user.getPhone()).getNickName().equals("管理员")){
return "pages/back/index" ;
}else {
String userId= String.valueOf(userService.selectUserByPhone(user.getPhone()).getUserId());
String nikeName= userService.selectUserByPhone(user.getPhone()).getNickName()+"";
Cookie id=new Cookie("userId",userId);
Cookie name=new Cookie("nikeName",nikeName);
response.addCookie(id);
response.addCookie(name);
id.setPath("/");
name.setPath("/");
int a= Integer.parseInt(user.getPhone()) ;
List<ShopCarVo> shopCar=shopCarService.selectShopCarByUserId(a);
model.addAttribute("count",shopCar);
model.addAttribute("User",userService.selectUserByPhone(user.getPhone()));
return "pages/front/Customers/ShopCar";
}
}
model.addAttribute("errorMsg","登录失败,连自己的手机号或密码都不知道吗!");
return "pages/front.login/loginPage";
}
登录后的控制台信息
有对应的cookie
cookie的时效: -1
cookie的Comment: null
cookie的Domain: null
cookie的Name: userId
cookie的Path: null
cookie的Value: 3
cookie的Secure: false
cookie的Version: 0
User(userId=null, nickName=null, realName=null, phone=3, password=3, lastLoginTime=null, lastLoginIp=null)
这是我的首页控制类获取登录后的cookie的控制类代码
@RequestMapping("/")
/*@ResponseBody*/
//表示返回json字符串给前端,否者return会返回名叫"hellow,bro"的前端页面
//因为如果方法类型为String会返回return中的字符的值的页面,而不是console输出
public String index(Model model, HttpServletRequest request, HttpServletResponse response){
Cookie[] cookies=request.getCookies();
for (Cookie cookie :cookies){
System.err.println(cookie.getValue());
if (cookie.getName().equals("nikeName")){
System.err.println("have userInfo");
System.err.println(cookie.getValue());
model.addAttribute("nikeName",cookie.getValue());
}else {
System.err.println("none userInfo");
model.addAttribute("nikeName","您好,请登录");
}
}
model.addAttribute("types",typeService.selectTypeByParentsId(0));
/* List<Goods> goods= goodsService.getGoodsByTypeId(11);*/
List<Goods> goods= goodsService.getAllGoods();
System.err.println(goods);
model.addAttribute("goods",goods);
return "index";
}
这是我的控制类的代码
@RequestMapping("/")
/*@ResponseBody*/
//表示返回json字符串给前端,否者return会返回名叫"hellow,bro"的前端页面
//因为如果方法类型为String会返回return中的字符的值的页面,而不是console输出
public String index(Model model, HttpServletRequest request, HttpServletResponse response){
Cookie[] cookies=request.getCookies();
for (Cookie cookie :cookies){
System.err.println(cookie.getValue());
if (cookie.getName().equals("nikeName")){
System.err.println("have userInfo");
System.err.println(cookie.getValue());
model.addAttribute("nikeName",cookie.getValue());
}else {
System.err.println("none userInfo");
model.addAttribute("nikeName","您好,请登录");
}
}
model.addAttribute("types",typeService.selectTypeByParentsId(0));
/* List<Goods> goods= goodsService.getGoodsByTypeId(11);*/
List<Goods> goods= goodsService.getAllGoods();
System.err.println(goods);
model.addAttribute("goods",goods);
return "index";
}
控制台代码(这里的宝宝巴士并不是我想要获取的nickName 这是我自己在浏览器中添加的 我预想的nickName应该是阿巴阿巴)
979ac195-91f5-426d-a632-717d78ba0ece
none userInfo
true
none userInfo
AXKAKUSSM4MVEFQSKBL6YVRAUWGP5L7IJ34RSRN4PCOA7SZC4ZLOKRKJBMFFNJBJQLVGCSCTCVDO6PUHKOKDJWZ4TM
none userInfo
宝宝巴士
have userInfo
宝宝巴士