it编程 > 数据库 > Access

后端解决跨域问题:已拦截跨源请求:同源策略禁止读取位于 http://localhost:8888/user/page?

166人参与 2024-08-06 Access

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);
    }
}

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

常见HTTP 500错误发生原因及解决办法剖析

08-06

Gitee ---- 在clone的时候需要用户密码 -- Incorrect username or password (access token)

08-06

【kerberos】org.apache.hadoop.security.AccessControlException: Client cannot authenticate via:[TOKEN,

08-04

华为Hybrid类型接口实现不同vlan之间通信

08-03

阿里云国际站充值:apache多网站权限

08-03

部署 Unified Access Gateway VMware UAG 3.5

08-02

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论