首页 > 其他分享 >springboot HandlerIntercept

springboot HandlerIntercept

时间:2022-12-30 14:46:22浏览次数:33  
标签:String RequestContext void request private HandlerIntercept public springboot

1、新建上下文类工具类

public class ContextHolder {
    private static final InheritableThreadLocal<RequestContext> REQUEST_CONTEXT_INHERITABLE_THREAD_LOCAL = new InheritableThreadLocal();

    public ContextHolder() {
    }

    public static RequestContext get() {
        return (RequestContext)REQUEST_CONTEXT_INHERITABLE_THREAD_LOCAL.get();
    }

    public static void set(RequestContext context) {
        REQUEST_CONTEXT_INHERITABLE_THREAD_LOCAL.set(context);
    }

    public static void remove() {
        REQUEST_CONTEXT_INHERITABLE_THREAD_LOCAL.remove();
    }
}

 2、新建上下文类

@Data
public class RequestContext {
    private String userPin;
    private String deviceId;
    private String appVersion;
    private String ip;
    private String requestId;
    private Cookie[] cookies;
    private Boolean forceBot;
    private Boolean iosFlag;
    private String venderId;
    private String currencyCode;
}

 3、新增拦截器

@Slf4j
public class RequestProcessInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        this.process(request, response);
        return true;
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        ContextHolder.remove();
        ThreadContext.clearAll();
    }

    private void setCookies(HttpServletRequest request, RequestContext requestContext) {
        Cookie[] cookies = request.getCookies();
        requestContext.setCookies(cookies);
    }
    private void process(HttpServletRequest request, HttpServletResponse response) {
        String platform = request.getHeader("x-api-platform");
        String deviceId = request.getHeader("x-api-device-token");
        String appVersion = request.getHeader("x-api-app-version");
        RequestContext context = ContextHolder.get() == null ? new RequestContext() : ContextHolder.get();
        this.setCookies(request, context);
        String pin = request.getHeader("pin");
        context.setUserPin(pin);
        ContextHolder.set(context);
    }
}

 4、新增拦截器配置

@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new RequestProcessInterceptor()).addPathPatterns("/**").excludePathPatterns("/login").order(0);
        registry.addInterceptor(new ResponseProcessInterceptor()).addPathPatterns("/**").excludePathPatterns("/login").order(1);
    }
}

 

标签:String,RequestContext,void,request,private,HandlerIntercept,public,springboot
From: https://www.cnblogs.com/niun/p/17014838.html

相关文章