CROS跨域配置异常

简介: 这是一个CORS跨域问题案例,前端(localhost:8080)尝试访问后端(localhost:9999)被阻止。后端已配置CORS过滤器,但`addAllowedOrigin`错误地设为`http://localhost:9999`。正确配置应为`http://localhost:8080`以允许前端请求。
Access to XMLHttpRequest at 'http://127.0.0.1:9999/login?username=1634856201@qq.com&password=wcl123456' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Login_Form.vue:97 
POST http://127.0.0.1:9999/login?username=1634856201@qq.com&password=wcl123456 net::ERR_FAILED 403 (Forbidden)

这是一个典型的CORS(跨源资源共享)问题,也就是说浏览器阻止了从 http://localhost:8080 发起到 http://127.0.0.1:9999/login 的请求。

首先我的后端部署IP端口为:localhost:9999

            前端部署IP端口为:localhost:8080

并且我在后端是有对跨域进行配置的:

/**
     * 创建并配置CORS过滤器,以允许来自指定来源的跨域请求。
     * 这个方法设置了过滤器的配置,包括允许的来源、请求头、请求方法和凭证。
     *
     * @return CorsFilter 返回配置好的CORS过滤器实例。
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();

        // 设置允许的来源为http://localhost:9999
        config.addAllowedOrigin("http://localhost:9999");

        // 设置允许所有的请求头
        config.addAllowedHeader("*");
        // 设置允许所有的请求方法
        config.addAllowedMethod("*");
        // 允许请求携带凭证(如cookies)
        config.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 将CORS配置应用到所有路径
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }

最后发现我的问题是出现在该行代码如下:

// 设置允许的来源为http://localhost:9999
config.addAllowedOrigin("http://localhost:9999");

这行代码的真实意思是配置允许接收处理哪一个IP端口发送过来的请求,而我的前端部署IP端口为localhost:8080,所以正确的配置应该是:

// 设置允许的来源为http://localhost:8080
config.addAllowedOrigin("http://localhost:8080");

这样的话就可以解决前端在localhost:8080端口发送请求给后端,后端在localhost:9999端口接收该跨域请求了。

相关文章
|
Java Spring
Spring Cloud OpenFeign 远程调用传递请求头信息
import feign.RequestInterceptor; import feign.RequestTemplate; import lombok.extern.slf4j.Slf4j; import org.springframework.util.Assert; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes;
791 0
|
Docker 容器
解决使用Dockerfile来build镜像时pip install遇到的BUG
解决使用Dockerfile来build镜像时pip install遇到的BUG
1840 5
解决使用Dockerfile来build镜像时pip install遇到的BUG
|
存储 分布式计算 资源调度
ARM+麒麟大数据环境搭建:Hadoop
ARM+麒麟大数据环境搭建:Hadoop
3839 0
ARM+麒麟大数据环境搭建:Hadoop
|
SQL 网络协议 关系型数据库
mysql 连接超时wait_timeout问题解决
com.mysql.jdbc.CommunicationsException: The last packet successfully received from the server was58129 seconds ago.The last packet sent successfully to the server was 58129 seconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or tes
|
7月前
|
SQL 关系型数据库 MySQL
批量操作性能飙升:从30秒到1秒的三种实战方法
业务系统中经常需要批量导入或更新大量数据(如Excel上传、定时同步)。许多开发人员采用循环单条执行的方式,导致1万条数据耗时30秒以上,严重影响用户体验。本文从数据库IO、事务开销、锁竞争三个角度分析单条操作的性能瓶颈,并给出三种优化方案:批量INSERT、LOAD DATA文件导入、批量UPDATE用临时表。每种方案均附实测数据对比与适用场景说明,帮助读者在1万\~100万行级别批量操作中选择最优策略。
|
人工智能 IDE 开发工具
JetBrains PyCharm 2025.1 发布 - 面向专业开发者的 Python IDE
JetBrains PyCharm 2025.1 (macOS, Linux, Windows) - 面向专业开发者的 Python IDE
1895 29
JetBrains PyCharm 2025.1 发布 - 面向专业开发者的 Python IDE
|
JavaScript 前端开发
uni-app中如何使用scss定义变量
使用SCSS在uni-app项目中定义和使用变量,不仅提高了样式代码的可维护性,还使得样式的修改和主题定制变得更加方便。只需更改变量值,就可以轻松改变整个应用的外观和风格。通过合理地组织和管理SCSS代码,可以大大提高开发效率和项目的可维护性。
993 9
|
安全 Linux Shell
docker运行centos提示Operation not permitted
通过上述步骤,可以有效排查和解决在Docker中运行CentOS容器时遇到的"Operation not permitted"错误。这些措施涵盖了从权限配置、安全策略到容器运行参数的各个方面,确保在不同环境和使用场景下都能顺利运行容器。如果你需要进一步优化和管理你的Docker环境
1938 3
|
XML 数据格式 Python
python 解析xml遇到xml.etree.ElementTree.ParseError: not well-formed (invalid token): |4-8
python 解析xml遇到xml.etree.ElementTree.ParseError: not well-formed (invalid token): |4-8
|
Web App开发 JavaScript 前端开发
Windows环境下 NVM 介绍、下载安装及使用详解
Windows环境下 NVM 介绍、下载安装及使用详解
2638 0