zoukankan      html  css  js  c++  java
  • spring mvc跨域设置(全局)

    //--------------第一步
    //spring 5版本全局配置方式 @Configuration @EnableWebMvc public class SpringMvcBeans implements WebMvcConfigurer{ @Override public void addCorsMappings(CorsRegistry registry) { if(!AppWebConf.isPro()) { registry.addMapping("/**"); } WebMvcConfigurer.super.addCorsMappings(registry); } } //spring 4版本全局配置方式 @Configuration @EnableWebMvc public class SpringMvcBeans extends WebMvcConfigurerAdapter{ @Override public void addCorsMappings(CorsRegistry registry) { //非正式环境支持跨域,适用于前后端分离开发阶段 if(!AppWebConf.isPro()) { registry.addMapping("/**"); } WebMvcConfigurer.super.addCorsMappings(registry); } //或者指定的url //registry.addMapping("/api/**") // .allowedOrigins("http://domain2.com") // .allowedMethods("PUT", "DELETE") // .allowedHeaders("header1", "header2", "header3") // .exposedHeaders("header1", "header2") // .allowCredentials(false).maxAge(3600); }



     //---------------------第二步

     //还需要自己在一个过滤器中写入这个代码,不然还是会浏览器抛出跨域异常

      //如果是不是正式环境

    if(!AppWebConf.isPro()) {

    response.setHeader("Access-Control-Allow-Origin", "*");

    }

      

  • 相关阅读:
    js小数点失精算法修正
    ActiveX控件之ActiveXObject is not defined
    js通过日期计算属于星期几
    标准日期格式化
    js阿拉伯数字转中文大写
    RPC 原理的前生今世
    大型网站架构系列:20本技术书籍推荐
    Zookeeper核心机制
    建造者模式
    模板方法模式
  • 原文地址:https://www.cnblogs.com/binz/p/8652181.html
Copyright © 2011-2022 走看看