SpringBoot中一个万能的Cors跨域Filter

SpringBoot中一个万能的Cors跨域Filter

跨域这个话题,群里几天能看到很多次,几乎都是一样的台词。

“这怎么回事啊?”
“怎么弄啊?”
“还是不行啊?”
“我按照XX做的啊?”

好像大家也不怎么关心跨域产生的原因,以及详细的去了解cors跨域。 :shushing_face:
行吧,那你就 把这个Filter丢到SpringBoot项目中,让Spring加载,它能解决 99% 以上的跨域问题

CorsFilter

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@WebFilter(urlPatterns = "/*")
@Order(-99999)
public class CorsFilter extends HttpFilter {


	/**
	 * 
	 */
	private static final long serialVersionUID = 2386571986045107652L;
	private static final String OPTIONS_METHOD = "OPTIONS";

	@Override
	protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {

		String origin = req.getHeader(HttpHeaders.ORIGIN);

		if (!StringUtils.isEmpty(origin)) {
			
			// 允许客户端的域
			res.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, origin);

			// 允许客户端提交的Header
			String requestHeaders = req.getHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
			if (!StringUtils.isEmpty(requestHeaders)) {
				res.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders);
			}
			
			// 允许客户端访问的Header
			res.addHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma");
			
			// 允许客户端携带凭证信息
			res.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
			
			// 允许客户端请求方法
			res.addHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, PUT, OPTIONS, DELETE");

			if (OPTIONS_METHOD.equalsIgnoreCase(req.getMethod())) {
				res.setStatus(HttpServletResponse.SC_NO_CONTENT);
				res.setContentType(MediaType.TEXT_HTML_VALUE);
				res.setCharacterEncoding("utf-8");
				res.setContentLength(0);
				res.addHeader(HttpHeaders.ACCESS_CONTROL_MAX_AGE, "1800");
				return;
			}
		}

		super.doFilter(req, res, chain);
	}
}


最后

你有兴趣了解一下跨域呢,可以看看这个