extjs 使用 Sencha CMD 6.5生成的应用程序Android的CORS发布POST请求(403禁止)

7ivaypg9  于 2022-11-04  发布在  Android
关注(0)|答案(1)|浏览(145)

我正在尝试生成一个应用程序Android(前端)与 Sencha CMD 6.5发送请求到Tomcat服务器(后端)。
问题是POST方法,当应用程序在标题中发送请求时,“Origin”参数设置为“file://”,tomcat的CORS拒绝请求(禁止403)。
集管

Accept: */*
Accept-Encoding: gzip,deflate
Accept-Language: en-US;q=0.9
Connection: keep-aliv(e)
Content-Length: 39
Host: 192.168.1.91: 8080
Origin: file: //
Referer: -
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0(Linux;Android7.0;MI5Build/NRD90M;wv)AppleWebKit/537.36(KHTML,likeGecko)Version/4.0Chrome/64.0.3282.137MobileSafari/537.36

在tomcat 8.0上的web.xml中定义的过滤器

<filter>
    <filter-name>CorsFilter</filter-name>
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
    <init-param>
      <param-name>cors.allowed.origins</param-name>
      <param-value>*</param-value>
    </init-param>
     <init-param>
      <param-name>cors.allowed.methods</param-name>
      <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
    </init-param> 
    <init-param>
      <param-name>cors.allowed.headers</param-name>
      <param-value>Cache-Control,Content-Language,Expires,Last-Modified,Pragma,Content-Type,X-Requested-With,Accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
    </init-param>
     <init-param>
        <param-name>cors.exposed.headers</param-name>
        <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
    </init-param> 
    <init-param>
      <param-name>cors.support.credentials</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>cors.preflight.maxage</param-name>
      <param-value>10</param-value>
    </init-param>
 </filter>

 <filter-mapping>
    <filter-name>CorsFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

有趣的是,使用GET方法时,在Origin参数中不设置任何内容,Tomcat CORS会接受它。
只有两种解决方案:
1.设置标题的参数Origin(我没有在任何论坛中找到任何东西,像这样,像senchaforum等)
1.修改TOMCAT 8的CORS过滤器。(我没有在任何论坛中找到任何东西,像这样,像senchaforum等)

byqmnocz

byqmnocz1#

解决方法是使用ContainerResponseFilter,而不是tomcat的过滤器。

import javax.ws.rs.ext.Provider;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;

@Provider
public class CORSResponseFilter implements ContainerResponseFilter {

@Override
public void filter(ContainerRequestContext creq, ContainerResponseContext cres) {

    MultivaluedMap<String, Object> headers = cres.getHeaders();
    headers.add("Access-Control-Allow-Origin", "*");
    headers.add("Access-Control-Allow-Headers", "Cache-Control,Content-Language,Expires,Last-Modified,Pragma,Content-Type,X-Requested-With,Accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers");
    headers.add("Access-Control-Allow-Credentials", "false");
    headers.add("Access-Control-Allow-Methods", "GET,POST,HEAD,OPTIONS,PUT");
    //headers.add("Access-Control-Max-Age", "10");
    }

}

我已经试过了,以防有人认为它会与原来的过滤器工作:

<init-param>
      <param-name>cors.support.credentials</param-name>
      <param-value>false</param-value>
</init-param>

欢迎伙计们,我希望它能在未来帮助到别人。
丹尼尔

相关问题