码迷,mamicode.com
首页 > 编程语言 > 详细

Spring boot2.0 与 2.0以前版本 跨域配置的区别

时间:2018-04-28 14:35:52      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:ati   app   mvcc   configure   contex   code   framework   author   升级   

一·简介

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 }  

 

Spring boot2.0 与 2.0以前版本 跨域配置的区别

标签:ati   app   mvcc   configure   contex   code   framework   author   升级   

原文地址:https://www.cnblogs.com/wang-yaz/p/8966869.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!