- 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.
- Getting Started with Citrix XenApp? 7.6
- UML和模式應用(原書第3版)
- Python高級機器學習
- Mastering Swift 2
- 你不知道的JavaScript(中卷)
- Getting Started with Python Data Analysis
- 小學生C++創意編程(視頻教學版)
- 大數據分析與應用實戰:統計機器學習之數據導向編程
- 硅谷Python工程師面試指南:數據結構、算法與系統設計
- 第一行代碼 C語言(視頻講解版)
- LabVIEW虛擬儀器入門與測控應用100例
- 持續集成與持續交付實戰:用Jenkins、Travis CI和CircleCI構建和發布大規模高質量軟件
- 移動增值應用開發技術導論
- 自學Python:編程基礎、科學計算及數據分析(第2版)
- Mastering Leap Motion