Redis实现布隆过滤器解析( 三 )

【4】构建布隆过滤器的拦截器
//拦截器,所有需要查看商品详情的请求必须先过布隆过滤器@Slf4jpublic class BloomFilterInterceptor implements HandlerInterceptor {@Autowiredprivate BloomRedisService bloomRedisService;@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {String currentUrl = request.getRequestURI();PathMatcher matcher = new AntPathMatcher();//解析出pathvariableMap<String, String> pathVariable = matcher.extractUriTemplateVariables("/pms/productInfo/{id}", currentUrl);//布隆过滤器存储在redis中if(bloomRedisService.includeByBloomFilter(RedisKeyPrefixConst.PRODUCT_REDIS_BLOOM_FILTER,pathVariable.get("id"))){return true;}/** 不在布隆过滤器当中 , 直接返回验证失败* 设置响应头*/response.setHeader("Content-Type","application/json");response.setCharacterEncoding("UTF-8");String result = new ObjectMapper().writeValueAsString(CommonResult.validateFailed("产品不存在!"));response.getWriter().print(result);return false;}}【5】将拦截器注册进SpringMVC中
@Configurationpublic class IntercepterConfiguration implements WebMvcConfigurer {@Overridepublic void addInterceptors(InterceptorRegistry registry) {//注册拦截器registry.addInterceptor(authInterceptorHandler()).addPathPatterns("/pms/productInfo/**");}@Beanpublic BloomFilterInterceptor authInterceptorHandler(){return new BloomFilterInterceptor();}}【Redis实现布隆过滤器解析】

推荐阅读