org.ebaysf.web的cors-filter使用,cors-filter跨域请求
================================
©Copyright 蕃薯耀 2020-11-26
https://www.cnblogs.com/fanshuyao/
cors-filter为第三方(ebay)组件。
一、官网地址
https://github.com/ebay/cors-filter
二、Springboot使用cors-filter
1、引入依赖
<dependency>
<groupId>org.ebaysf.web</groupId>
<artifactId>cors-filter</artifactId>
<version>1.0.1</version>
</dependency>
2、配置类
import javax.servlet.Filter;
import org.ebaysf.web.cors.CORSFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 过滤器配置类
*/
@Configuration
public class HttpFilterConfig {
/**
* org.ebaysf.web cors-filter
* @return
*/
@Bean
public FilterRegistrationBean<Filter> corsFilter() {
FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
registration.setFilter(new CORSFilter());//org.ebaysf.web.cors.CORSFilter
//这个可直接不配置
//Defaults: true
registration.addInitParameter("cors.support.credentials", "true");
//这个可直接不配置
//Defaults: * (Any origin is allowed to access the resource).
registration.addInitParameter("cors.allowed.origins", "http://127.0.0.1:7010");
//这个可直接不配置
//Defaults: GET,POST,HEAD,OPTIONS
registration.addInitParameter("cors.allowed.methods", "GET,POST");
//这个可直接不配置
// Default