跨域解决方案 CORS
   
Cross-Origin-Resources-Sharing
配置一个configurer
package com.example.mango.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
@Configuration
public class WebMvcConfigurer implements org.springframework.web.servlet.config.annotation.WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")   //允许跨域访问的路径
        .allowedOrigins("*")  //允许跨域访问的源
        .allowedMethods("POST","GET","PUT","OPTIONS","DELETE")   //允许的请求方法
        .maxAge(168000)  //预检间隔时间
        .allowedHeaders("*")   //允许头部设置
        .allowCredentials(true);//允许发送coolie
    }
}
    对于一个跨域请求,它的头部字段中加入了Origin
    
    服务器会在响应中添加Access-Contro-Allow-Origin字段,如果接受该跨域请求,该字段会有正确的值
   
 
版权声明:本文为weixin_43948626原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。