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 }  
  • 相关阅读:
    gThumb 3.1.2 发布,支持 WebP 图像
    航空例行天气预报解析 metaf2xml
    Baruwa 1.1.2 发布,邮件监控系统
    Bisect 1.3 发布,Caml 代码覆盖测试
    MoonScript 0.2.2 发布,基于 Lua 的脚本语言
    Varnish 入门
    快速增量备份程序 DeltaCopy
    恢复模糊的图像 SmartDeblur
    Cairo 1.12.8 发布,向量图形会图库
    iText 5.3.4 发布,Java 的 PDF 开发包
  • 原文地址:https://www.cnblogs.com/wang-yaz/p/8966869.html
Copyright © 2011-2022 走看看