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", "*");

    }

      

  • 相关阅读:
    CentOS 6.5、6.7 设置静态 ip 教程
    Nagios
    zabbix
    xml 的 <![CDATA["URL"]]>
    Don't make a promise when you are in Joy. Don't reply when you are Sad.Don't take decisions when you are Angry.Think Twice.Act Wise.
    DNS-2
    APM
    Time crumbles things; everything grows old under the power of Time and is forgotten through the lapse of Time
    CentOS 7 配置静态 ip
    AOP
  • 原文地址:https://www.cnblogs.com/binz/p/8652181.html
Copyright © 2011-2022 走看看