1.上篇文章讲到,当处理复杂请求时,需要在pom文件引入如下依赖,需在web.xml配置一个过滤器org.apache.catalina.filters.CorsFilter,那么这个过滤器为我们做了哪些工作呢?,下面对源码进行分析解读.
<!--支持跨域-->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>7.0.78</version>
<scope>provided</scope>
</dependency>
2.首先看doFilter中的初始化代码,即我们可以在web.xml中初始化我们的数据.如果我们没有设置,默认允许什么的来源
public void init(FilterConfig filterConfig) throws ServletException {
this.parseAndStore("*", "GET,POST,HEAD,OPTIONS", "Origin,Accept,X-Requested-With,Content-Type,Access-Control-Request-Method,Access-Control-Request-Headers", "", "true", "1800", "true");
默认的设置 //参数1: 表示允许所有的请求源 参数2: 表示允许的http请求方式 参数3:表示允许携带的请求头,
if(filterConfig != null) {
String configAllowedOrigins = filterConfig.getInitParameter("cors.allowed.origins");//支持请求的来源
String configAllowedHttpMethods = filterConfig.getInitParameter("cors.allowed.methods");//支持的方法
String configAllowedHttpHeaders = filterConfig.getInitParameter("cors.allowed.headers");//支持的头信息
String configExposedHeaders = filterConfig.getInitParameter("cors.exposed.headers");
String configSupportsCredentials = filterConfig.getInitParameter("cors.support.credentials");
String configPreflightMaxAge = filterConfig.getInitParameter("cors.preflight.maxage");
String configDecorateRequest = filterConfig.getInitParameter("cors.request.decorate");
this.parseAndStore(configAllowedOrigins, configAllowedHttpMethods, configAllowedHttpHeaders, configExposedHeaders, configSupportsCredentials, configPreflightMaxAge, configDecorateRequest);
}
}
3.doFilter方法,可以看出标红部分是判断请求的类型的关键,根据不同的访问类型,将做不同的处理方式,checkRequestType(request)方法根据请求所携带的信息判断访问的类型是那种,返回的结果有1:NOT_CORS不是跨域请求 2.INVALID_CORS无效的跨域 3.ACTUAL 4.SIMPLE,5.PRE_FLIGHT预请求类型
a.当处理SIMPLE和ACTUAL请求时,调用handleSimpleCORS()方法,此方法会根据用户设置或者默认设置添加head信息如默认情况("Access-Control-Allow-Origin", "*");即允许所有的跨域源,添加完头信息,放行进入下个过滤器.
b.当处理PRE_FLIGHT请求时,调用handlePreflightCORS()方法,添加头部信息,放回浏览器
c.当处理NOT_CORS请求时,直接放行
d.当处理INVALID_CORS请求时,response.setStatus(403);返回浏览器
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
if(servletRequest instanceof HttpServletRequest && servletResponse instanceof HttpServletResponse) {
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
CorsFilter.CORSRequestType requestType = this.checkRequestType(request);//关键,判断访问的类型
if(this.decorateRequest) {
decorateCORSProperties(request, requestType);
}
switch(CorsFilter.SyntheticClass_1.$SwitchMap$org$apache$catalina$filters$CorsFilter$CORSRequestType[requestType.ordinal()]) {
case 1:
this.handleSimpleCORS(request, response, filterChain);
break;
case 2:
this.handleSimpleCORS(request, response, filterChain);
break;
case 3:
this.handlePreflightCORS(request, response, filterChain);
break;
case 4:
this.handleNonCORS(request, response, filterChain);
break;
default:
this.handleInvalidCORS(request, response, filterChain);
}
} else {
throw new ServletException(sm.getString("corsFilter.onlyHttp"));
}
}