springboot 怎样解决 No 'Access"/>
springboot 怎样解决 No 'Access
前后端分离项目有时会遇到 No 'Access-Control-Allow-Origin' 错误
后端解决代码:
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 java.util.Arrays;@Configuration
public class CorsConfig {@Beanpublic CorsFilter corsFilter(){final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();final CorsConfiguration config = new CorsConfiguration();config.setAllowCredentials(true);config.setAllowedHeaders(Arrays.asList("*"));config.setAllowedOrigins(Arrays.asList("*"));config.setAllowedMethods(Arrays.asList("*"));config.setMaxAge(10000L);source.registerCorsConfiguration("/**", config);return new CorsFilter(source);}
}
更多推荐
springboot 怎样解决 No 'Access
发布评论