package com.demo1.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
/**
* 实现WebMvcConfigurer方法,用于跨域请求
*/
public class CorsConfig implements WebMvcConfigurer{
/**
* registry.addMapping("/**") 将CorsRegistry对象中的映射路径设置为"/**",表示这个配置将应用于所有路径。
* .allowedHeaders("*") 允许来自所有域的请求。
* .allowedMethods("GET","POST") 表示允许的请求的头,默认请求的所有的头消息
*.maxAge(3600); 请求有限期
* @param registry
*/
@Override
public void addCorsMappings(CorsRegistry registry){
registry.addMapping("/**")
.allowedHeaders("*")
.allowedOrigins("*")
.allowedMethods("GET","POST","OPTIONS", "DELETE", "PUT", "PATCH")
.maxAge(3600);
}
}
springboot的跨域解决方式 作者:沐沐
最新推荐文章于 2024-05-21 11:58:13 发布