• 幂等 zuul的Filter实现


    通过zuul的过滤器 filter实现

    //app 幂等过滤
    @SuppressWarnings("all")
    @Order(Ordered.HIGHEST_PRECEDENCE)
    @WebFilter(filterName = "ValidAppIdempotent", urlPatterns = {
            "/app/api/inbound/inboundEdit","/app/api/outbound/outboundEdit"
            ,"/app/api/inbound/inboundEdit_NonNote","/app/api/outbound/outboundEdit_NonNote"}
            )
    public class ValidAppIdempotent extends OncePerRequestFilter {
        private static final Logger logger = LoggerFactory.getLogger(ValidToken.class);
        public final static String ERROR_REPEATSUBMIT = "Repeated submission";
     
        @Value("${appSign}")
        private String appSign;
        @Autowired
        protected StringRedisTemplate idempotentTemplate;
     
     
        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
                throws ServletException, IOException {
            //获取 body 中post过来的数据
            String contentType = request.getContentType();
            if (StringUtils.isNotBlank(contentType) && contentType.indexOf(MediaType.APPLICATION_JSON.toString()) >= 0) {
                request = new BodyReaderHttpServletRequestWrapper(request);
            }
            String data = null;
            try {
                data = NetUtil.getBodyString(request);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //获取 url 中的数据
            String sign = request.getParameter("sign");
            String signMD5 = MD5.getSign(data, appSign);
            if (Boolean.parseBoolean(idempotentTemplate.opsForValue().get(signMD5))) {
                throw new ErrorSignException(ERROR_REPEATSUBMIT);
            } else {
                idempotentTemplate.opsForValue().set(signMD5, "true", ComContants.IDEMPOTENT_EXTIME, TimeUnit.SECONDS);
            }
     
            filterChain.doFilter(request, response);
     
        }
    }

    测试:

    @RequestMapping(value = "inboundEdit", method = RequestMethod.POST)
        public ApiResult<String> inboundEdit(@RequestBody ApiRequest<String> request) {
            ApiResult<String> r = new ApiResult<String>();
            return r;
        }
  • 相关阅读:
    FFT学习及简单应用(一点点详细)
    Codeforces 1131 (div 2)
    【NOI2012】魔幻棋盘
    【hdu多校联考第二场】Odd Shops
    【Helvetic Coding Contest 2018】B2. Maximum Control (medium)
    解线性同余方程组
    Math teacher's homework
    【NOIP模拟赛】一道挖掉背景的数学题
    逆向基础-模块隐藏之断链
    解决请求筛选模块被配置为拒绝包含的查询字符串过长的请求
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/11112904.html
Copyright © 2020-2023  润新知