我有一个由Tomcat托管的静态网站。
如何为我的网站设置标题,例如:Access-Control-Allow-Origin: *
它们都是静态文件,而不是任何servlet应用程序。
答案 0 :(得分:64)
如果它是静态站点,那么从Tomcat 7.0.41开始,您可以通过built-in filter轻松控制CORS行为。
您唯一需要做的就是编辑web.xml
中的全局CATALINA_HOME/conf
并添加过滤器定义:
<!-- ================== Built In Filter Definitions ===================== --> ... <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> <!-- ==================== Built In Filter Mappings ====================== -->
请注意,Firefox不喜欢Access-Control-Allow-Origin: *
并请求凭据(Cookie):when responding to a credentialed request, server must specify a domain, and cannot use wild carding.
答案 1 :(得分:12)
这是一个非常基本的过滤器,它将添加CORS标头。请注意,默认情况下,这将启用所有域和方法,因此您应该自定义它以满足您的需要。
它还需要是web.xml中的第一个过滤器。
package com.conductiv.api.listener;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CORSFilter implements Filter {
public void destroy() {
}
public static String VALID_METHODS = "DELETE, HEAD, GET, OPTIONS, POST, PUT";
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletRequest httpReq = (HttpServletRequest) req;
HttpServletResponse httpResp = (HttpServletResponse) resp;
// No Origin header present means this is not a cross-domain request
String origin = httpReq.getHeader("Origin");
if (origin == null) {
// Return standard response if OPTIONS request w/o Origin header
if ("OPTIONS".equalsIgnoreCase(httpReq.getMethod())) {
httpResp.setHeader("Allow", VALID_METHODS);
httpResp.setStatus(200);
return;
}
} else {
// This is a cross-domain request, add headers allowing access
httpResp.setHeader("Access-Control-Allow-Origin", origin);
httpResp.setHeader("Access-Control-Allow-Methods", VALID_METHODS);
String headers = httpReq.getHeader("Access-Control-Request-Headers");
if (headers != null)
httpResp.setHeader("Access-Control-Allow-Headers", headers);
// Allow caching cross-domain permission
httpResp.setHeader("Access-Control-Max-Age", "3600");
}
// Pass request down the chain, except for OPTIONS
if (!"OPTIONS".equalsIgnoreCase(httpReq.getMethod())) {
chain.doFilter(req, resp);
}
}
public void init(FilterConfig config) throws ServletException {
}
}
答案 2 :(得分:3)
您需要添加Filter
以添加其他标头并将其配置为所有路径
<filter>
<filter-name>header</filter-name>
<filter-class>...</filter-class>
</filter>
<filter-mapping>
<filter-name>header</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
答案 3 :(得分:2)
请注意,要使用值*
配置CORS过滤器,除了启用文件管理器外,还需要添加参数<param-name>cors.allowed.origins</param-name>
的配置,如下所示(在Johannes Jander answer之上构建):
<!-- ================== Built In Filter Definitions ===================== -->
...
<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>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- ==================== Built In Filter Mappings ====================== -->