搜索
您的当前位置:首页SpringBoot允许跨域请求

SpringBoot允许跨域请求

来源:乌哈旅游

在前后端分离的架构中,允许跨域请求是一个很重要的设置。SpringBoot项目中允许跨域请求比较简单,只需要我们定义好配置类即可。

在com.example.csdn.config包里面创建CorsConfig类,然后设置允许跨域请求。

package com.example.csdn.config;

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 CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH")
                .maxAge(3600);
    }

}

因篇幅问题不能全部显示,请点此查看更多更全内容

Top