- Spring 5.0 Microservices(Second Edition)
- Rajesh R V
- 227字
- 2021-07-02 19:45:05
Enabling cross origin for microservices interactions
Browsers are generally restricted when client-side web applications running from one origin request data from another origin. Enabling cross origin access is generally termed as CORS (Cross Origin Resource Sharing).
This is particularly important when dealing with microservices, such as when the microservices run on separate domains, and the browser tries to access these microservices from one browser after another:

The preceding example showcases how to enable cross origin requests. With microservices, since each service runs with its own origin, it will easily get into the issue of a client-side web application, which consumes data from multiple origins. For instance, a scenario where a browser client accesses customers from the customer microservice, and order history from the order microservices is very common in microservices world.
Spring Boot provides a simple declarative approach for enabling cross origin requests.
The following code example shows how to use a microservice to enable cross origin:
@RestController
class GreetingController{
@CrossOrigin
@RequestMapping("/")
Greet greet(){
return new Greet("Hello World!");
}
}
By default, all origins and headers are accepted. We can further customize the cross origin annotations by giving access to a specific origin as follows. The @CrossOrigin annotation enables a method or a class to accept cross origin requests:
@CrossOrigin("http://mytrustedorigin.com")
Global CORS could be enabled by using the WebMvcConfigurer bean, and customizing the addCorsMappings (CorsRegistry registry) method.
- C++面向?qū)ο蟪绦蛟O(shè)計(第三版)
- C程序設(shè)計簡明教程(第二版)
- 在最好的年紀學Python:小學生趣味編程
- Java高并發(fā)核心編程(卷2):多線程、鎖、JMM、JUC、高并發(fā)設(shè)計模式
- Web交互界面設(shè)計與制作(微課版)
- Building Minecraft Server Modifications
- 微信小程序開發(fā)解析
- FPGA Verilog開發(fā)實戰(zhàn)指南:基于Intel Cyclone IV(進階篇)
- MySQL入門很輕松(微課超值版)
- Java語言程序設(shè)計教程
- 貫通Tomcat開發(fā)
- Hacking Android
- INSTANT JQuery Flot Visual Data Analysis
- Java自然語言處理(原書第2版)
- Zend Framework 2 Cookbook