https://www.cnblogs.com/520playboy/p/7306008.html
1. For projects with front-end and back-end separation, 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.
2. 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.
3. 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.
@Configuration public class MyWebAppConfigurer extends WebMvcConfigurerAdapter{ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**"); }
Our path can be configured in addMapping. /** stands for all paths.
Of course, other properties can also be modified
@Configuration public class MyWebAppConfigurer extends WebMvcConfigurerAdapter{ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**") .allowedOrigins("http://192.168.1.97") .allowedMethods("GET", "POST") .allowCredentials(false).maxAge(3600); }
The above two are for global configuration. If you want to be more detailed, you can also use the @CrossOrigin annotation in the controller class.
@CrossOrigin(origins = "http://192.168.1.97:8080", maxAge = 3600) @RequestMapping("rest_index") @RestController public class IndexController{
In this way, you can specify that all methods in the controller can handle requests from http:19.168.1.97:8080.
1. For projects with front-end and back-end separation, 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.
2. 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.
3. 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.
@Configuration public class MyWebAppConfigurer extends WebMvcConfigurerAdapter{ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**"); }
Our path can be configured in addMapping. /** stands for all paths.
Of course, other properties can also be modified
@Configuration public class MyWebAppConfigurer extends WebMvcConfigurerAdapter{ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**") .allowedOrigins("http://192.168.1.97") .allowedMethods("GET", "POST") .allowCredentials(false).maxAge(3600); }
The above two are for global configuration. If you want to be more detailed, you can also use the @CrossOrigin annotation in the controller class.
@CrossOrigin(origins = "http://192.168.1.97:8080", maxAge = 3600) @RequestMapping("rest_index") @RestController public class IndexController{
In this way, you can specify that all methods in the controller can handle requests from http:19.168.1.97:8080.