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 }  
  • 相关阅读:
    我不写博客的原因就是cnblogs不好用
    使用KeePass愉快的来管理你的密码
    Visual studio常用的code snippets
    日常工作小贴士
    win10下面visual studio, sublime ctrl+shift+f快捷键失效的原因
    Markdown常用语法对应
    常见的Message Queue应用场景
    二分查找(binary search)
    在ROS中使用花生壳的域名服务
    排序算法 选择排序(selection sort)
  • 原文地址:https://www.cnblogs.com/wang-yaz/p/8966869.html
Copyright © 2011-2022 走看看