**
全局跨域解决方案2
**
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Bean
public WebMvcConfigurer corsConfigs(){
return new WebMvcConfigurer(){
public void addCorsMappings(CorsRegistry registry){
registry.addMapping("/**")
.allowedHeaders("*")
.allowedMethods("*")
// .allowCredentials(true)
.allowedOrigins("*")
.maxAge(3600L);
}
};
}
private CorsConfiguration corsConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*"); //允许所有域名访问
corsConfiguration.addAllowedHeader("*"); //允许所有请求头
corsConfiguration.addAllowedMethod("*"); //允许所有的请求类型
corsConfiguration.setMaxAge(3600L);
corsConfiguration.setAllowCredentials(true); //允许请求携带验证信息(cookie)
return corsConfiguration;
}
}
本文介绍了如何在SpringMVC应用中使用全局CORS配置,允许所有域名、请求头和方法,同时支持携带验证信息。
1621

被折叠的 条评论
为什么被折叠?



