Solve cross-domain problems through cors protocol in springboot

 

For projects with separate front-end and back-end, if the front-end project and the back-end project are deployed in two different domains, cross-domain problems will inevitably arise.

For cross-domain problems, the first solution we may think of is jsonp, and I have basically dealt with cross-domain problems in the past.

However, the jsonp method also has shortcomings. Whether it is for the front end or the back end, the writing method is different from our usual ajax writing method, and the back end also needs to be changed accordingly. In addition, the jsonp method can only pass parameters through the get request method. Of course, there are other shortcomings. For this, I am not in a hurry to use the jsonp method to solve the cross-domain problem. I searched for other methods on the Internet. This is what this article mainly talks about, solving cross-domain problems through the cors protocol in springboot.

 

CORS protocol
Author  : K_Biao
Link: http://www.imooc.com/article/7719 Source
: MOOC

Cors Protocol

 

New in H5: Cross-Origin Resource Sharing. Through it, our developers (mainly referring to back-end developers) can decide whether resources can be accessed across domains.

cors is a w3c standard, which allows browsers (currently not supported below ie8) to send xmlHttpRequest requests like our servers of different origins, and we can continue to use ajax for request access.

For specific articles about the cors protocol, you can refer to http://www.ruanyifeng.com/blog/2016/04/cors.html This article is quite good.

How to solve cross-domain problems through cors protocol in springboot

The springmvc4.2 version adds support for cors.

At present, the projects I do are basically developed with springboot, so I will post the use in springboot here.

 

[java]  view plain copy  
 
  1. @Configuration  
  2. public class MyWebAppConfigurer extends WebMvcConfigurerAdapter{  
  3.   
  4.     @Override  
  5.     public void addCorsMappings(CorsRegistry registry) {  
  6.         registry.addMapping("/**");  
  7.     }  

可以在addMapping中配置我们的路径。/**代表所有路径。

 

当然也可以修改其它属性

 

[java]  view plain  copy
 
  1. @Configuration  
  2. public class MyWebAppConfigurer extends WebMvcConfigurerAdapter{  
  3.   
  4.     @Override  
  5.     public void addCorsMappings(CorsRegistry registry) {  
  6.            registry.addMapping("/api/**")  
  7.            .allowedOrigins("http://192.168.1.97")  
  8.            .allowedMethods("GET""POST")  
  9.            .allowCredentials(false).maxAge(3600);  
  10.     }  

以上两种,都是针对全局配置,如果你想做到更细致也可以使用@CrossOrigin这个注解在controller类中使用。

 

 

[java]  view plain  copy
 
  1. @CrossOrigin(origins = "http://192.168.1.97:8080", maxAge = 3600)  
  2. @RequestMapping("rest_index")  
  3. @RestController  
  4. public class IndexController{  

这样就可以指定该controller中所有方法都能处理来自http:19.168.1.97:8080中的请求。

http://blog.csdn.net/u011890101/article/details/74253842

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326307667&siteId=291194637