0
我正在使用彈簧引導和角度2項目。 如果我在本地測試,每件事情都可以正常工作。 當我在Apache Tomcat服務器部署的.war,它顯示了這個著名的錯誤:無法解決請求的資源上沒有'Access-Control-Allow-Origin'標頭
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin .
我搜索到Tomcat的網站。 加入此項:
<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>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>
不會改變任何事情。
而且,我嘗試這樣做:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().authorizeRequests().antMatchers(HttpMethod.OPTIONS, "/*").permitAll();
http.httpBasic();
http.csrf().disable();
}