赞
踩
因springboot框架通常用于前后端分离项目,因此需配置后台允许跨域访问(具体看注释),配置类如下,将该类加入工程中即可。
- 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;
- /**
- * @author suntongxin
- * Create on 2017年7月6日下午8:05:19
- * All right reserved
- */
- @Configuration
- public class CorsConfig {
- private CorsConfiguration buildConfig() {
- CorsConfiguration corsConfiguration = new CorsConfiguration();
- corsConfiguration.addAllowedOrigin("*"); //允许任何域名
- corsConfiguration.addAllowedHeader("*"); //允许任何头
- corsConfiguration.addAllowedMethod("*"); //允许任何方法
- return corsConfiguration;
- }
-
- @Bean
- public CorsFilter corsFilter() {
- UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
- source.registerCorsConfiguration("/**", buildConfig()); //注册
- return new CorsFilter(source);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。