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

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.

主站蜘蛛池模板: 靖西县| 略阳县| 宜川县| 肥乡县| 福海县| 甘孜县| 大邑县| 大方县| 连平县| 开封市| 措勤县| 年辖:市辖区| 哈密市| 工布江达县| 河池市| 鹤岗市| 筠连县| 汉沽区| 天柱县| 宁河县| 酉阳| 阿坝| 辉南县| 贡嘎县| 桓台县| 克什克腾旗| 大同县| 德州市| 江口县| 晋中市| 厦门市| 肇庆市| 新乡县| 繁昌县| 大悟县| 榆中县| 太仆寺旗| 宜章县| 惠安县| 南陵县| 巴彦淖尔市|