*html
tomcat 解決跨域,java
1,聽說tomcat7.0.40以上纔有自帶的CrosFilter,在WEB-INF/web.xml中配置便可:nginx
<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,yourdefinedname</param-value> </init-param> <init-param> <param-name>cors.exposed.headers</param-name> <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials,Access-Control-Allow-Headers</param-value> </init-param> </filter> <filter-mapping> <filter-name>CorsFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2,tomcat7.0.40如下解決跨域,web.xml中web
<filter> <filter-name>CorsFilter</filter-name> <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class> <init-param> <param-name>cors.allowOrigin</param-name> <param-value>*</param-value> </init-param> <init-param> <param-name>cors.supportedMethods</param-name> <param-value>GET, POST, HEAD, PUT, DELETE</param-value> </init-param> <init-param> <param-name>cors.supportedHeaders</param-name> <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified,Access-Control-Request-Headers</param-value> </init-param> <init-param> <param-name>cors.exposedHeaders</param-name> <param-value>Set-Cookie</param-value> </init-param> <init-param> <param-name>cors.supportsCredentials</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CorsFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
,須要引入兩個包,若是是用maven,則pom.xml中能夠加上下面這句,保存後自動會下載:apache
<dependency> <groupId>com.thetransactioncompany</groupId> <artifactId>cors-filter</artifactId> <version>1.7</version> <scope>runtime</scope> </dependency>
maven倉庫下載地址能夠這樣配置,{maven_home}/conf/settings.xml:api
<mirror> <id>sensordata</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://central.maven.org/maven2/</url> </mirror> <mirror> <id>repo2</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo2.maven.org/maven2/</url> </mirror> <mirror> <id>ibiblio1</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://mirrors.ibiblio.org/maven2/</url> </mirror> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors>
3,也能夠自定義跨域過濾器,這個應該不針對tomcat版本跨域
第一步,寫一個類 CrosFilter.javatomcat
import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletResponse; public class CrosFilter implements Filter{ @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletResponse res = (HttpServletResponse) response; res.setContentType("text/html;charset=UTF-8"); res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); res.setHeader("Access-Control-Max-Age", "0"); res.setHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type"); res.setHeader("Access-Control-Allow-Credentials", "true"); res.setHeader("XDomainRequestAllowed","1"); chain.doFilter(request, response); } @Override public void destroy() { // TODO Auto-generated method stub } }
第二步,在web.xml中配置過濾器app
<filter> <filter-name>cros</filter-name> <filter-class>com.zlfund.openapi.webserver.filter.CrosFilter</filter-class> </filter> <filter-mapping> <filter-name>cros</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
*********cors
這幾種均可以解決,可是公司預生產就一直存在 跨域,原來它還通過了 nginx,在那裏也要配置
server {
listen 443;
server_name myapi.com;
access_log /var/log/nginx/myapiaccess.log myapi_access;
error_log /var/log/nginx/myapierror.log;
more_set_headers "Access-Control-Allow-Origin: $http_origin";
# more_set_headers "Access-Control-Allow-Credentials : true";
# more_set_headers "Access-Control-Allow-Methods: GET,POST,HEAD,OPTIONS,PUT";
# more_set_headers "Access-Control-Expose-Headers: Access-Control-Allow-Headers,Access-Control-Allow-Origin,Access-Control-Allow-Credentials";
# more_set_headers "Access-Control-Allow-Headers: retdatatype,content-type,access-control-request-headers,accept,access-control-request-method,origin,x-requested-with";
more_set_headers "Access-Control-Allow-Headers: retdatatype,content-type";
使用的是 https://myapi.com 訪問,因此在443 這裏配置
*