मैं नीचे दिखाए गए WebMvcConfigurerAdapter
के माध्यम से विश्व स्तर पर CORS को कॉन्फ़िगर करने का प्रयास कर रहा हूं। परीक्षण करने के लिए मैं एक बाहरी सेवा अनुकरण करने के लिए बनाए गए एक छोटे नोड ऐप के माध्यम से अपना एपीआई एंडपॉइंट मार रहा हूं। जब मैं इस तरीके का प्रयास प्रतिसाद सही हेडर शामिल नहीं है औरस्प्रिंग ग्लोबल सीओआरएस कॉन्फ़िगरेशन काम नहीं कर रहा है लेकिन नियंत्रक स्तर कॉन्फ़िगरेशन
XMLHttpRequest cannot load http://localhost:8080/api/query/1121. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:333' is therefore not allowed access.
वैश्विक कॉन्फ़िग के साथ विफल
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/query/**")
.allowedOrigins("*")
.allowedHeaders("*")
.allowCredentials(true);
}
}
हालांकि जब मैं @CrossOrigin
एनोटेशन का उपयोग तो यह उचित हेडर के साथ जवाब सिर्फ ठीक काम करता है की तरह ।
@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")
@RestController
@RequestMapping(value = "/api/query", produces = MediaType.APPLICATION_JSON_VALUE)
public class QueryController {
......
}
उत्पादन
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:333
क्या मैं वैश्विक config काम करने के लिए याद आ रही है (यहां दिए गए निर्देशों का पालन किया https://spring.io/blog/2015/06/08/cors-support-in-spring-framework)। मुझे लगता है कि नियंत्रक को एनोटेट करने के बाद से मुझे कुछ आसान याद आ रहा है।
शायद '.allowedOrigins ("*")।अनुमति हैडर्स ("*") वैश्विक कॉन्फ़िगरेशन में अनावश्यक हैं –