Apache Solr Options方法预检问题

时间:2018-11-22 07:05:27

标签: angular apache solr cross-domain

我直接从角度使用solr,并遇到交叉问题。我通过添加交叉过滤器解决了

<filter>
   <filter-name>cross-origin</filter-name>
   <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
   <init-param>
     <param-name>allowedOrigins</param-name>
     <param-value>*</param-value>
   </init-param>
   <init-param>
     <param-name>allowedMethods</param-name>
     <param-value>GET,POST,OPTIONS,DELETE,PUT,HEAD</param-value>
   </init-param>
   <init-param>
     <param-name>allowedHeaders</param-name>
     <param-value>origin, content-type, accept</param-value>
   </init-param>
 </filter>

 <filter-mapping>
   <filter-name>cross-origin</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>

但是当我用带有请求主体的post命中solr时,它的触发选项偏偏并且收到错误消息说“内容流不正确”,后来发现选项偏偏没有得到正确处理,所以我最终添加了自己的过滤器

@Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {

        logger.info("Options Filter executed");

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        System.out.println("CORSFilter HTTP Request: " + request.getMethod());


((HttpServletResponse) servletResponse).addHeader("Access-Control-Allow-Headers", "*");
((HttpServletResponse) servletResponse).addHeader("Access-Control-Allow-Origin", "*");
((HttpServletResponse) servletResponse).addHeader("Access-Control-Allow-Methods","GET, OPTIONS, HEAD, PUT, POST");

        HttpServletResponse resp = (HttpServletResponse) servletResponse;


        if (request.getMethod().equals("OPTIONS")) {
            logger.info("Options request is handled and responded ok");
            resp.setStatus(HttpServletResponse.SC_ACCEPTED);
            return;
}


        chain.doFilter(request, servletResponse);
}

我想知道这是唯一的方法,否则还有其他方法吗

0 个答案:

没有答案