官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 安阳县| 柳河县| 黄山市| 呼伦贝尔市| 上蔡县| 四平市| 郴州市| 侯马市| 乌兰县| 广宁县| 包头市| 东平县| 伊宁县| 全南县| 自贡市| 瑞丽市| 临漳县| 宁德市| 凤凰县| 乐都县| 阿勒泰市| 永丰县| 上虞市| 美姑县| 屏东县| 营口市| 保定市| 武夷山市| 博湖县| 兴山县| 新竹市| 临沂市| 沽源县| 新民市| 崇信县| 长宁县| 新乡市| 中超| 丽江市| 永嘉县| 荥经县|