Springboot解决跨域请求

Springboot解决跨域请求

springboot解决跨域请求,直接编写WebMvcConfigurer实现即可,并且重写addCorsMappings方法即可,在该方法中添加你需要允许的请求类型配置信息。

package com.xymtop.api.config;

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

/**
 * @ClassName : CorsConfig
 * @Description : 跨域请求
 * @Author : 肖叶茂
 * @Date: 2022/12/2  21:40
 */
// 请求跨域
@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //添加映射路径
        registry.addMapping("/**")
                //是否发送Cookie
                .allowCredentials(true)
                //设置放行哪些原始域   SpringBoot2.4.4下低版本使用.allowedOrigins("*")
                .allowedOriginPatterns("*")
                //放行哪些请求方式
                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"})
                //.allowedMethods("*") //或者放行全部
                //放行哪些原始请求头部信息
                .allowedHeaders("*")
                //暴露哪些原始请求头部信息
                .exposedHeaders("*");
    }
}

猜你喜欢

转载自blog.csdn.net/qq_35889508/article/details/128160733