跨域
跨域请求是指浏览器脚本文件在发送请求时,脚本所在的服务器和请求的服务器地址不一样。跨域是有浏览器的同源策略造成的,是浏览器对JavaScript施加的安全限制,
同源策略:是指协议、域名、端口都要相同,其中有一个不同都会产生跨域
SpringBoot框架RESTful接口解决跨域
此处是有配置文件的方式来解决的
package com.prereadweb.config.cors;
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;
/**
* @Description: 接口跨域
* @author: Yangxf
* @date: 2019/4/19 14:43
*/
@Configuration
public class CorsConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址
corsConfiguration.addAllowedHeader("*"); // 2 设置访问源