zoukankan      html  css  js  c++  java
  • 幂等 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;
        }
  • 相关阅读:
    JSON基本操作
    常用的windowd属性和对象
    手动建立数据库连接的BaseDAO
    注意1:图像插值理论的理解
    Python的Scikit-learn如何选择合适的机器学习算法?
    spark-sklearn(spark扩展scikitlearn)
    Tips-Windows 10【多桌面视窗】操作
    jupyter notebook快捷键速查手册
    使用IntelliJ IDEA进行Python远程调试的需求(未完)
    Bazel构建工具的安装
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/11112904.html
Copyright © 2011-2022 走看看