zoukankan      html  css  js  c++  java
  • Spring boot2.0 与 2.0以前版本 跨域配置的区别

    一·简介

    spring boot升级到2.0后发现继承WebMvcConfigurerAdapter实现跨域过时了,那我们就紧随潮流。

    二·全局配置

      2.0以前 支持跨域请求代码:

     1 import org.springframework.context.annotation.Configuration;  
     2 import org.springframework.web.servlet.config.annotation.CorsRegistry;  
     3 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;  
     4   
     5 /** 
     6  * 说明:跨域请求 
     7  * 
     8  * @author WangBin 
     9  * @version v1.0 
    10  * @date 2018/1/21/ 
    11  */  
    12 @Configuration  
    13 public class CorsConfig extends WebMvcConfigurerAdapter {  
    14   
    15     @Override  
    16     public void addCorsMappings(CorsRegistry registry) {  
    17         registry.addMapping("/**")  
    18                 .allowedOrigins("*")  
    19                 .allowCredentials(true)  
    20                 .allowedMethods("*")  
    21                 .maxAge(3600);  
    22     }  
    23 }  

    2.0版本如下:

     1 import org.springframework.context.annotation.Configuration;  
     2 import org.springframework.web.servlet.config.annotation.CorsRegistry;  
     3 import org.springframework.web.servlet.config.annotation.EnableWebMvc;  
     4 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;  
     5   
     6 /** 
     7  * 说明:跨域请求 
     8  * 
     9  * @author WangBin 
    10  * @version v1.0 
    11  * @date 2018/1/21/ 
    12  */  
    13 @Configuration  
    14 @EnableWebMvc  
    15 public class CorsConfig implements WebMvcConfigurer {  
    16   
    17     @Override  
    18     public void addCorsMappings(CorsRegistry registry) {  
    19         //设置允许跨域的路径  
    20         registry.addMapping("/**")  
    21                 //设置允许跨域请求的域名  
    22                 .allowedOrigins("*")  
    23                 //是否允许证书 不再默认开启  
    24                 .allowCredentials(true)  
    25                 //设置允许的方法  
    26                 .allowedMethods("*")  
    27                 //跨域允许时间  
    28                 .maxAge(3600);  
    29     }  
    30 }  
  • 相关阅读:
    hive函数之~字符串函数
    hive函数之~条件函数
    JSONP使用及注意事项小结
    css命名管理混乱?不妨试试BEM
    【移动端debug-6】如何做一个App里的web调试小工具
    ES6学习笔记(五):Class和Module
    ES6学习笔记(三):与迭代相关的新东东
    ES6学习笔记(四):异步操作
    ES6学习笔记(二):引用数据类型
    CORS跨域资源共享简述
  • 原文地址:https://www.cnblogs.com/wang-yaz/p/8966869.html
Copyright © 2011-2022 走看看