166人参与 • 2024-08-06 • Access
已拦截跨源请求:同源策略禁止读取位于 http://localhost:8888/user/page?pagenum=1&pagesize=10&username=&email=&address= 的远程资源。(原因:cors 头缺少 'access-control-allow-origin')。状态码:200。
加上一个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);
}
}
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论