欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

怎样解决跨域?,所谓同源指的是两个页

来源: javaer 分享于  点击 5807 次 点评:89

怎样解决跨域?,所谓同源指的是两个页


跨域的原因

  跨域是是因为浏览器的同源策略限制,是浏览器的一种安全机制,服务端之间是不存在跨域的。 所谓同源指的是两个页面具有相同的协议、主机和端口,三者有任一不相同即会产生跨域。

解决跨域

配置类重写addCorsMappings方法

1、CorsConfig配置类添加@Configuration注解 2、继承WebMvcConfigurer 3、重写addCorsMappings方法 完成后再启动项目时会自动加载  
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 跨域请求配置
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry){
        //设置允许跨域的路径
        registry.addMapping ("/**")
                //设置允许跨域请求的域名
                .allowedOriginPatterns ("*")
                //是否允许证书
                .allowCredentials (true)
                //设置允许的方法
                .allowedMethods ("GET","POST", "PUT", "DELETE", "OPTIONS")
                //设置允许的header属性
                .allowedHeaders ("*")
                //允许跨域时间
                .maxAge (3600);
    }

}

 

相关文章

    暂无相关文章
相关栏目:

用户点评