1. For projects separated by front-end and back-end projects, if the front-end project and the back-end project are deployed in two different domains, it will inevitably cause cross-domain problems.
For cross-domain problems, the first solution we think of is jsonp, and I basically dealt with cross-domain problems in the past.
However, the jsonp method also has shortcomings. Whether for the front-end or back-end, the writing method is different from our usual Ajax writing method, and the back-end also needs to make corresponding changes. Moreover, the jsonp method can only pass parameters through get request, and of course there are other shortcomings. In response to this, I am not in a hurry to use jsonp method to solve cross-domain problems. I go online to find other methods, which is what this article mainly talks about, and solve cross-domain problems through the cors protocol in springboot.
2. Cors Agreement
New features in H5: Cross-Origin Resource Sharing. Through it, our developers (mainly backend developers) can decide whether resources can be accessed across domains.
Cors is a W3C standard, which allows browsers (currently below ie8 cannot be supported). Like our servers from different sources, we can continue to use ajax for request access.
For specific articles about cors protocol, please refer to http://www.VeVB.COM/article/83093.htm. This article is quite good.
3. How to solve cross-domain problems through cors protocol in springboot
SpringMvc4.2 version adds support for cors.
At present, the projects I'm doing are basically developed in springboot, so I'll post the use in springboot here.
@Configurationpublic class MyWebAppConfigurer extends WebMvcConfigurerAdapter{ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**"); }Our path can be configured in addMapping. /** represents all paths.
Of course, other properties can also be modified
@Configurationpublic 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 all 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")@RestControllerpublic class IndexController{ This allows you to specify that all methods in the controller can handle requests from http:19.168.1.97:8080.
Summarize
The above is the editor’s introduction to how to solve cross-domain problems through the Cors protocol in springboot. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!