package com.nq.config;
|
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Configuration;
|
import org.springframework.web.cors.CorsConfiguration;
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
import org.springframework.web.filter.CorsFilter;
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
@Configuration
|
public class MyCorsFilter {
|
|
/*@Override
|
public void addCorsMappings(CorsRegistry corsRegistry){
|
*//**
|
* 所有请求都允许跨域,使用这种配置就不需要
|
* 在 interceptor 中配置 header 了
|
*//*
|
corsRegistry.addMapping("/*")
|
//是否发送Cookie
|
.allowCredentials(true)
|
.allowedOrigins("*")
|
.allowedHeaders("*")
|
.allowedMethods("*");
|
}*/
|
private CorsConfiguration corsConfig(){
|
CorsConfiguration corsConfiguration = new CorsConfiguration();
|
corsConfiguration.addAllowedHeader("*");
|
corsConfiguration.addAllowedMethod("*");
|
corsConfiguration.addAllowedOrigin("*");
|
corsConfiguration.setMaxAge(3600L);
|
corsConfiguration.setAllowCredentials(true);
|
return corsConfiguration;
|
}
|
@Bean
|
public CorsFilter corsFilter(){
|
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
source.registerCorsConfiguration("/**",corsConfig());
|
return new CorsFilter(source);
|
}
|
|
}
|