springboot 项目跨域问题 CORS

package com.example.demo.cors;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
 * springboot 项目间跨域问题  CORS
 * @author 
 *
 */
@Configuration
public class CustomCorsConfiguration {

	@Bean
	public WebMvcConfigurationSupport getConfigurer() {
		return new WebMvcConfigurationSupport() {

			@Override
			protected void addCorsMappings(CorsRegistry registry) {
				registry.addMapping("/api/**")//允许访问的接口
					.allowedOrigins(new String[] {"http://localhost:8081"});//允许哪些站点访问该接口
			}
			
		};
	}
}

  

posted @ 2019-02-12 16:59  xfma  阅读(152)  评论(0编辑  收藏  举报