|
| 1 | +package com.baeldung.reactive.cors.annotated.controllers; |
| 2 | + |
| 3 | +import org.springframework.web.bind.annotation.CrossOrigin; |
| 4 | +import org.springframework.web.bind.annotation.PutMapping; |
| 5 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 6 | +import org.springframework.web.bind.annotation.RestController; |
| 7 | + |
| 8 | +import reactor.core.publisher.Mono; |
| 9 | + |
| 10 | +@CrossOrigin(value = { "http://allowed-origin.com" }, allowedHeaders = { "Baeldung-Another-Allowed" }, maxAge = 900) |
| 11 | +@RestController |
| 12 | +@RequestMapping("/cors-on-controller") |
| 13 | +public class CorsOnClassController { |
| 14 | + |
| 15 | + @PutMapping("/regular-endpoint") |
| 16 | + public Mono<String> corsDisabledEndpoint() { |
| 17 | + return Mono.just("Regular endpoint"); |
| 18 | + } |
| 19 | + |
| 20 | + @CrossOrigin |
| 21 | + @PutMapping("/cors-enabled-endpoint") |
| 22 | + public Mono<String> corsEnabledEndpoint() { |
| 23 | + return Mono.just("CORS enabled endpoint"); |
| 24 | + } |
| 25 | + |
| 26 | + @CrossOrigin({ "http://another-allowed-origin.com" }) |
| 27 | + @PutMapping("/cors-enabled-origin-restrictive-endpoint") |
| 28 | + public Mono<String> corsEnabledOriginRestrictiveEndpoint() { |
| 29 | + return Mono.just("CORS enabled endpoint - Origin Restrictive"); |
| 30 | + } |
| 31 | + |
| 32 | + @CrossOrigin(allowedHeaders = { "Baeldung-Allowed" }) |
| 33 | + @PutMapping("/cors-enabled-header-restrictive-endpoint") |
| 34 | + public Mono<String> corsEnabledHeaderRestrictiveEndpoint() { |
| 35 | + return Mono.just("CORS enabled endpoint - Header Restrictive"); |
| 36 | + } |
| 37 | + |
| 38 | + @CrossOrigin(exposedHeaders = { "Baeldung-Exposed" }) |
| 39 | + @PutMapping("/cors-enabled-exposed-header-endpoint") |
| 40 | + public Mono<String> corsEnabledExposedHeadersEndpoint() { |
| 41 | + return Mono.just("CORS enabled endpoint - Exposed Header"); |
| 42 | + } |
| 43 | + |
| 44 | + @PutMapping("/cors-enabled-mixed-config-endpoint") |
| 45 | + @CrossOrigin(allowedHeaders = { "Baeldung-Allowed", "Baeldung-Other-Allowed" }, exposedHeaders = { "Baeldung-Allowed", "Baeldung-Exposed" }, maxAge = 3600) |
| 46 | + public Mono<String> corsEnabledHeaderExposedEndpoint() { |
| 47 | + return Mono.just("CORS enabled endpoint - Mixed Config"); |
| 48 | + } |
| 49 | +} |
0 commit comments