//解码
List<CartItem> cart = null;
Cookie[] cookies = request.getCookies();
boolean flag = true;
for (Cookie cookie : cookies) {
if(EGO_CART_COOKIE.equals(cookie.getName())){
String value = cookie.getValue();
Decoder decoder = Base64.getDecoder();
byte[] decode = decoder.decode(value);
String jsonString = new String(decode);
cart = JsonUtils.jsonToList(jsonString, CartItem.class);
flag = false;
break;
}
}
//加入cookie之前,Base64编码
String json = JsonUtils.objectToJson(cart);
Encoder encoder = Base64.getEncoder();
String encodeToString = encoder.encodeToString(json.getBytes());
Cookie c = new Cookie(EGO_CART_COOKIE, encodeToString);
//暴露cookie,所有人可以访问
//这种方式不安全,我们需要通过domain属性来设置一个域名保护
c.setPath("/");
response.addCookie(c);