排骨瘦肉丁 2019-12-17 10:49 采纳率: 0%
浏览 493

后端接口和h5交互,复杂请求只有OPTIONS,然后就没有POST请求了,为何?

后端接口服务在跟h5交互的时候POST请求,复杂请求会有一次OPTIONS请求,但是小概率出现只有OPTIONS,然后就没有POST了,OPTIONS是返回正常的状态码,这种问题出现的原因是什么呢?目前貌似只有上传文件接口出现了该问题

                public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        // 允许的访问方法
        response.setHeader("Access-Control-Allow-Methods","POST, GET, PUT, OPTIONS, DELETE, PATCH");
        // Access-Control-Max-Age 用于 CORS 相关配置的缓存
        response.setHeader("Access-Control-Max-Age", "86400");
        response.setHeader("Access-Control-Allow-Headers","token,Origin, X-Requested-With, Content-Type, Accept");

        if(HttpMethod.OPTIONS.name().equals(request.getMethod().toUpperCase())){
            response.setStatus(HttpStatus.SC_NO_CONTENT);
            filterChain.doFilter(request,response);
        }

        boolean check = false;
        String token = request.getHeader("token");

        if(StringUtils.isNotBlank(token)){
            long mobile = redisProxy.getMobileByToken(token);
            if(mobile > 0){
                // token验证成功
                check = true;
                request.setAttribute(Constant.REQUEST_UM5, Md5Util.getMd5(mobile));
            }
        }
        if(check){
            filterChain.doFilter(request,response);
        }else {
            //验证不通过 返回需要登录的状态码
            ResponseView responseView = new ResponseView(ResultType.NEED_LOGIN);
            String resultStr = JSON.toJSONString(responseView);
            PrintWriter writer = null;
            try {
                writer = response.getWriter();
                writer.write(resultStr);
                writer.flush();
            } catch (IOException e) {
                log.error("filter error:{}",e);
            }finally {
                if(writer != null ){
                    writer.close();
                }
            }
        }
    }

  • 写回答

1条回答 默认 最新

  • 关注

    springmvc里面有没有配置跨域相关的的filter之类的,如CorsFilter等等

    @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
                throws ServletException, IOException {
            response.addHeader("Access-Control-Allow-Origin", "*");
            response.addHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,HEAD,OPTIONS");
            response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type,Authorization, X-Trace-ID,Accept");
            response.addHeader("Content-Type", "application/json;charset=utf-8");
            filterChain.doFilter(request, response);
        }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀