【Gulimall+】拦截器HandlerInterceptor前后置处理目标方法、RequestInterceptor解决Feign远程调用的请求头丢失、上下文丢失问题

HandlerInterceptor拦截器

Cart的HandlerInterceptor

com/atguigu/gulimall/cart/interceptor/CartInterceptor.java

 /**
     * 浏览器有一个cookie:user-key,标识用户身份,一个月后过期
     * 如果第一次使用jd的购物车功能,都会给一个临时的用户身份
     * 浏览器以后保存,每次访问都会带上这个cookie
     *
     * 登录 session 有
     * 没登录,按照cookie中的user-key来做
     * 第一次:如果没有临时用户,帮忙创建一个临时用户 --> 使用拦截器
     * @return 
     */
/**
 * 在执行目标方法之前,判断用户的登录状态,并封装给Controller目标请求
 */
@Component
public class CartInterceptor implements HandlerInterceptor {
    public static ThreadLocal<UserInfo> threadLocal = new ThreadLocal<>(); //同一个线程共享数据

    /**
     * 目标方法执行之前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserInfo userInfo = new UserInfo();
        // 查看session中是否保存了用户的值
        MemberRespVo member = JSON.parseObject(JSON.toJSONString(request.getSession().getAttribute(AuthServerConstant.LOGIN_USER)),MemberRespVo.class);
        if (member != null) {
            // 用户登录
            userInfo.setUserId(member.getId());
        }
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                // 拿到cookie名字进行判断如果包含 user-key 就复制到userInfo中
                if (name.equals(CartConstant.TEMP_USER_COOKIE_NAME)) {
                    userInfo.setUserKey(cookie.getValue());
                    userInfo.setTempUser(true);
                }
            }
        }
        // 如果没有临时用户一定要分配一个临时用户
        if (StringUtils.isEmpty(userInfo.getUserKey())) {
            String uuid = UUID.randomUUID().toString();
            userInfo.setUserKey(uuid);
        }
        // 全部放行
        threadLocal.set(userInfo);
        return true;
    }

    /**
     * 业务执行之后,分配临时用户,让浏览器保存
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 拿到用户信息
        UserInfo userInfo = threadLocal.get();
        // 如果没有临时用户一定要保存一个临时用户
        if (!userInfo.isTempUser()) {
            // 持续的延长临时用户的过期时间
            Cookie cookie = new Cookie(CartConstant.TEMP_USER_COOKIE_NAME, userInfo.getUserKey());
            cookie.setDomain("gulimall.com");
            cookie.setMaxAge(CartConstant.TEMP_USER_COOKIE_TIMEOUT);
            response.addCookie(cookie);
        }
    }
}

光有HandlerInterceptor拦截器不行,得添加到WebMvcConfigurer的InterceptorRegistry才能起作用

@Configuration
public class GulimallWebConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CartInterceptor()).addPathPatterns("/**");//拦截所有请求
    }
}

Order的HandlerInterceptor


@Component
public class LoginUserInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberRespVo> loginUser = new ThreadLocal<>(); //同一个线程共享数据
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

//        "/order/order/status/{orderSn}"
        String requestURI = request.getRequestURI();
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        boolean match =antPathMatcher.match("/order/order/status/**", requestURI);
        boolean match1 = antPathMatcher.match("/payed/notify", requestURI);//支付成功的异步回调
        if (match || match1) { //匹配直接放行
            return true;
        }

        MemberRespVo attribute = JSON.parseObject(JSON.toJSONString(request.getSession().getAttribute(AuthServerConstant.LOGIN_USER)),MemberRespVo.class);
        if (attribute != null) {
            loginUser.set(attribute);

            return true;
        } else {
            //没登录去登录
            request.getSession().setAttribute("msg","没登录请先登录");
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }
    }
}

同样也要添加到InterceptorRegistry,这儿就不粘贴了。

同一线程共享数据ThreadLocal

public static ThreadLocal<MemberRespVo> loginUser = new ThreadLocal<>();
//放置同一线程要共享的数据数据
loginUser.set(attribute);
//同一线程中取共享数据
MemberRespVo memberRespVo = LoginUserInterceptor.loginUser.get();

请求丢失问题

Feign远程调用

Feign调用流程:
1.构造请求数据,将对象转为json

RequestTemplate template = buildTemplateFromArgs.create(argv)

2.发送请求进行执行(执行成功会解码响应数据
executeAndDecode(template)
3.执行请求会有重试机制

while(true){
	try{
	executeAndDecode(template);
	}catch(){
		try{retry.continueOrPropagate(e);}catch(){throw ex};
		continue;
	}
}

Feign远程调用丢失请求头问题
在这里插入图片描述远程调用时创建了一个新request,自然就没有请求头中的数据,那咱调用方加上请求拦截器补上请求头数据
com/atguigu/gulimall/order/config/GuliFeignConfig.java

@Configuration
public class GuliFeignConfig {
    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                //1.RequestContextHolder拿到刚进来的这个请求
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = attributes.getRequest();//老请求
                //同步请求头,Cookie
                String cookie = request.getHeader("Cookie");
                //给新请求同步老请求的cookie
                requestTemplate.header("Cookie", cookie);
//                System.out.println("feign远程之前先进行RequestInterceptor.apply");
            }
        };
    }
}

PS:RequestInterceptor是以ThreadLocal实现的(在RequestContextHolder中)

异步编排

Feign异步情况丢失上下文问题
在这里插入图片描述由于是在不同的线程中执行,所以72中的ThreadLocal在子线程101,102中并不起作用,那就将主线程的请求属性取出然后分别放到各异步子线程。

		MemberRespVo memberRespVo = LoginUserInterceptor.loginUser.get(); //
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

        CompletableFuture<Void> getAddressFuture = CompletableFuture.runAsync(() -> {
            //每一个线程都共享之前的请求数据
            RequestContextHolder.setRequestAttributes(requestAttributes); // 解决异步调用请求头丢失
            // 1.远程查询所有的收获地址列表
            List<MemberAddressVo> address;
            try {
                address = memberFeignService.getAddress(memberRespVo.getId());
//                System.out.println(address);
                orderConfirmVo.setAddress(address);
            } catch (Exception e) {
                log.warn("\n远程调用会员服务失败 [会员服务可能未启动]");
            }
        }, executor);

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星空•物语

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值