use of cn.e3mall.common.pojo.E3Result in project e3mall by colg-cloud.
the class LoginInterceptor method preHandle.
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
/*
* 从cookie中取token
* token存在: 调用sso服务取用户信息
* 取到: 是登录状态, 需要把用户信息写入request, 判断cookie中是否有购物车数据, 如果有就合并到服务端
* 取不到: 用户登录已过期, 需要登录
* token不存在: 未登录状态, 跳转到sso系统的登录页面, 用户登录成功后, 跳转到当前请求的url
*/
String token = CookieUtils.getCookieValue(request, TOKEN_KEY);
if (StringUtils.isBlank(token)) {
// token不存在
response.sendRedirect(SSO_URL + "/page/login?redirect=" + request.getRequestURL());
// 拦截
return false;
}
// token存在
E3Result e3Result = tokenService.getUserByToken(token);
if (!e3Result.isSuccess()) {
// 取不到
response.sendRedirect(SSO_URL + "/page/login?redirect=" + request.getRequestURL());
// 拦截
return false;
}
TbUser tbUser = (TbUser) e3Result.getData();
request.setAttribute("user", tbUser);
// 判断cookie中是否有购物车数据
String jsonString = CookieUtils.getCookieValue(request, COOKIE_CART, true);
if (StringUtils.isNotBlank(jsonString)) {
// 合并购物车
cartService.mergeCart(tbUser.getId(), JSON.parseArray(jsonString, TbItem.class));
}
return true;
}
Aggregations