Spring Boot与CorsRegistry联合使用

1、现在越来越多的开发使用了请后端分离,或者由于一些网站多个模块的部署,需要跨域请求。为此,使用CorsRegistry。

注:若地址里面的协议、域名和端口号均相同则属于同源。

2、关于CorsRegistry的使用

(1)引入相关的依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>  
(2)配置

package com.hehe.yyweb.config;

import org.springframework.context.annotation.Bean;
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 GlobalCorsConfig {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public v