1、问题
已拦截跨源请求:同源策略禁止读取位于 http://localhost:8888/user/page?pagenum=1&pagesize=10&username=&email=&address= 的远程资源。(原因:cors 头缺少 'access-control-allow-origin')。状态码:200。
2、解决方法
加上一个corsconfig配置类
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.web.cors.corsconfiguration;
import org.springframework.web.cors.urlbasedcorsconfigurationsource;
import org.springframework.web.filter.corsfilter;
@configuration
public class corsconfig {
// 当前跨域请求最大有效时长。这里默认1天
private static final long max_age = 24 * 60 * 60;
@bean
public corsfilter corsfilter() {
urlbasedcorsconfigurationsource source = new urlbasedcorsconfigurationsource();
corsconfiguration corsconfiguration = new corsconfiguration();
corsconfiguration.addallowedorigin("*"); // 1 设置访问源地址
corsconfiguration.addallowedheader("*"); // 2 设置访问源请求头
corsconfiguration.addallowedmethod("*"); // 3 设置访问源请求方法
corsconfiguration.setmaxage(max_age);
source.registercorsconfiguration("/**", corsconfiguration); // 4 对接口配置跨域设置
return new corsfilter(source);
}
}
发表评论