Spring Boot项目设置跨域
一、跨域设置
新建一个配置类
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
/**
* @author wangshida
* @datetime 2023-02-15 上午11:20
* @desc 跨域
* @menu
*/
public class CorsFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse httpResponse = (HttpServletResponse) response;
HttpServletRequest httpRequest = (HttpServletRequest) request;
String originHeader = httpRequest.getHeader("Origin");
//允许某个域名发起跨域请求
httpResponse.addHeader("Access-Control-Allow-Origin", StringUtils.isEmpty(originHeader)?"*":originHeader);
//设置允许Cookie
httpResponse.addHeader("Access-Control-Allow-Credentials", "true");
//设置允许跨域请求的方法
httpResponse.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
//允许跨域请求包含content-type
httpResponse.addHeader("Access-Control-Allow-Headers", "Origin,X-Requested-With,Content-Type,Accept,X-Token");
//表示可以缓存时长,单位秒,表示隔30分钟才发起预检请求
httpResponse.addHeader("Access-Control-Max-Age", "3600");
//跨域的预检请求直接返回
if ("OPTIONS".equals(httpRequest.getMethod())) {
response.getWriter().println("ok");
return;
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
新增FilterConfig
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author wangshida
* @datetime 2023-02-15 上午11:20
* @desc
* @menu
*/
@Configuration
public class FilterConfig {
/**
* 跨域设置
*/
@Bean
public FilterRegistrationBean corsFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new CorsFilter());
registration.addUrlPatterns("/*");
return registration;
}
}