如果大家不知道我下面说的redis-cookie单点登录,那么请看我的上篇文章基于cookie-redis实现的单点登录点击打开链接。
购物车实现原理
利用上篇所讲的cookie-redis中的用户信息中的username来作为reids中的key,value来存储用户的信息。
细说
要想将物品加入购物车那么必定要先登录之后才能加入购物车,因为我们 需要cookie中存放的用户的name(为什么不能用uuid作为key那,因为用户每次登录的时候uuid都是不一样的那么必定会使用户加入购物车的商品不复存在 )。所以我们需要首先进行拦截
package com.chenfeng.ego.portal.interceptro;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.smallming.commons.util.CookieUtils;
import com.smallming.commons.util.HttpClientUtil;
import com.smallming.commons.util.JsonUtils;
import com.smallming.ego.pojo.TbUser;
public class CartInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("执行了拦截器");
try {
//如果用户没有登录跳转到登录页面
String cookieValue = CookieUtils.getCookieValue(request, "TT_TOKEN");
if(cookieValue!=null&&!cookieValue.equals("")){
//还可以向redis取内容
Map param = new HashMap<>();
param.put("callback","abc");
String result = HttpClientUtil.doPost("http://localhost:8084/user/token/"+cookieVa