我有一个角度客户端,它应该从 Oracle Rest 数据服务中获取数据。ORDS 在处理身份验证的 tomcat 中运行。
如果我用 curl 提出请求,它可以工作。我有 ac# webapi,我编写了一个控制器来从 ORDS => 获取数据也可以工作。所以我的角度客户端从 c# webapi => ORDS 接收数据,但不直接从 ORDS 接收数据。
login(username, password) {
let b64 = btoa(username + ':' + password);
localStorage.setItem('currentUser', b64);
let headers = new HttpHeaders();
headers = headers.append('Accept', 'application/json');
headers = headers.append('Authorization', 'Basic ' + b64);
return this.http.get<any>(`URL to ORDS`, {headers} ).subscribe(result => {
console.log(JSON.stringify(result));
});
}
我的tomcat 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>http://localhost:4200/</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>*</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>
在服务器返回的那一刻
Access to XMLHttpRequest at 'URL to ORDS' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.