在Jetty中可以正常工作(无Cors问题):
CorsFilter corsFilter = new CorsFilter(getContext());
corsFilter.setAllowedOrigins(new HashSet(Arrays.asList("*")));
corsFilter.setAllowedHeaders(Sets.newHashSet(
Constants.HEADER_MASTER_KEY,
Constants.HEADER_MASTER_KEY.toLowerCase(),
Constants.HEADER_API_KEY,
Constants.HEADER_API_KEY.toLowerCase(),
Constants.HEADER_APP_ID,
Constants.HEADER_APP_ID.toLowerCase(),
Constants.HEADER_AUTH_TOKEN,
Constants.HEADER_AUTH_TOKEN.toLowerCase(),
Constants.HEADER_ACCEPT,
Constants.HEADER_ACCEPT.toLowerCase(),
Constants.HEADER_ACL_READ,
Constants.HEADER_ACL_READ.toLowerCase(),
Constants.HEADER_ACL_WRITE,
Constants.HEADER_ACL_WRITE.toLowerCase(),
Constants.HEADER_CONTENT_TYPE,
Constants.HEADER_CONTENT_TYPE.toLowerCase()));
corsFilter.setAllowedHeaders(new HashSet<>(Arrays.asList("*")));
corsFilter.setAllowingAllRequestedHeaders(true);
corsFilter.setAllowedCredentials(true);
corsFilter.setSkippingResourceForCorsOptions(true);
corsFilter.setDefaultAllowedMethods(new HashSet(Arrays.asList(Method.GET,
Method.POST,Method.PUT,Method.DELETE,Method.OPTIONS)));
但是在Tomcat中,由于CORS问题,客户端无法访问资源。这有什么问题吗?
我也尝试过:
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
但没有效果。