前后端分离的跨域问题解决:No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.

简介: 本文介绍了解决前后端分离项目中跨域问题的方法,包括添加`CorsConfig`配置类和重写`WebMvcConfigurer`接口的`addCorsMappings`方法,允许前端请求访问后端资源,并提供了具体的代码示例。
  • 一、 添加 CorsConfig 配置类
  • 二、重写 webMvcConfigurer 中的 addCorsMapping

后端全局配置解决跨域问题的方式有下面两种,二选一即可。

一、 添加 CorsConfig 配置类

①定义一个类,加上@Configuration注解,表明这个类是SpringBoot的配置类;
②在CorsFilter中,定义相关跨域配置。

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

二、重写 webMvcConfigurer 中的 addCorsMapping

①定义一个类,加上@Configuration注解,表明这个类是SpringBoot的配置类;实现WebMvcConfigurer接口
②重写addCorsMappings方法,并且调用相应的方法,允许前端的请求发送到后端。

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
   


    // 重写 addCoresMapping 方法

    @Override
    public void addCorsMappings(CorsRegistry registry) {
   
        registry.addMapping("/**") // 1.对所有请求路径格式进行跨域处理
                .allowedHeaders("*")        // 2.允许任意请求头
                .allowedMethods("*")        // 3.允许任意请求方法 get post head delete put
                .maxAge(1800)               // 4.设置跨域有效期
                .allowedOrigins("http://localhost:8080"); // 5.支持的域名  --- 这里我的前端是8080端口,后端是9090
    }
}

在这里插入图片描述

在这里插入图片描述


相关文章
|
5月前
|
前端开发 对象存储 数据安全/隐私保护
阿里云前端直传has been blocked by CORS policy: Response to preflight request doesn‘t pass access control
阿里云前端直传has been blocked by CORS policy: Response to preflight request doesn‘t pass access control
646 2
|
6月前
|
JavaScript 前端开发
has been blocked by CORS policy: The ‘Access-Control-Allow-Origin‘ header contains multiple values ‘
has been blocked by CORS policy: The ‘Access-Control-Allow-Origin‘ header contains multiple values ‘
145 0
|
API
【已解决】No ‘Access-Control-Allow-Origin‘ header is present on the requested resource
No ‘Access-Control-Allow-Origin‘ header is present on the requested resource
392 0
【已解决】No ‘Access-Control-Allow-Origin‘ header is present on the requested resource
|
11月前
|
安全 JavaScript 前端开发
No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.
No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.
|
JavaScript Java 应用服务中间件
Vue 跨域问题 的几种解决办法 (No ‘Access-Control-Allow-Origin‘ header is present on the requested resource)
Vue 跨域问题 的几种解决办法 (No ‘Access-Control-Allow-Origin‘ header is present on the requested resource)
1991 0
Vue 跨域问题 的几种解决办法 (No ‘Access-Control-Allow-Origin‘ header is present on the requested resource)
|
应用服务中间件 API nginx
解决跨域问题 Response to preflight request doesn't pass access control check
解决跨域问题 Response to preflight request doesn't pass access control check
11534 1
|
API
百度API调用JSONP解决跨越问题 been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header
百度API调用JSONP解决跨越问题 been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header
220 0
|
前端开发 JavaScript
ajax请求的重定向处理--Request header field x-requested-with is not allowed by Access-Control-Allow-Header
ajax请求的重定向处理--Request header field x-requested-with is not allowed by Access-Control-Allow-Header
483 0
|
Web App开发 存储 安全
新版本Chrome同源策略、跨域问题处理No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.
新版本Chrome同源策略、跨域问题处理No ‘Access-Control-Allow-Origin‘ header is present on the requested resource.
796 0
(原因:不允许有多个 ‘Access-Control-Allow-Origin‘ CORS 头)
(原因:不允许有多个 ‘Access-Control-Allow-Origin‘ CORS 头)
364 0