如何将 CORS 功能添加到我的 http 网关 (spring-integration-dsl)?
How can I add CORS functionality into my http-gateway (spring-integration-dsl)?
我有一个 reactjs 前端,它基于 spring-集成向我的 api 发送请求。
我遇到的问题是,我不知道如何在我的网关中绑定 CORS 功能。
我试过这样的东西
@Bean
public CrossOrigin cors(){
CrossOrigin c = new CrossOrigin();
c.setOrigin("/**");
return c;
}
@Bean
public IntegrationFlow httpGetTest() {
return IntegrationFlows.from(httpGetGateTest()).channel("http.test.channel").handle("testEndpoint", "hello").get();
}
@Bean
public MessagingGatewaySupport httpGetGateTest() {
HttpRequestHandlingMessagingGateway handler = new HttpRequestHandlingMessagingGateway();
handler.setRequestMapping(createMapping(new HttpMethod[]{HttpMethod.GET}, "/test"));
handler.setCrossOrigin(cors());
handler.setHeaderMapper(headerMapper());
return handler;
}
要求:
axios.get('http://localhost:8080/test')
.then(res=>{console.log(res)})
我的端点returns"Hello World"
Failed to load resource: the server responded with a status of 415 ()
Access to XMLHttpRequest at 'http://localhost:8080/test' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
首先,请确保您的客户端确实发送了 Origin
HTTP 请求 header。
否则 CORS 过滤不会应用于请求:https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
尽管听起来好像它就在那里:from origin 'http://localhost:3000'
.
考虑将您的 setOrigin("/**")
更改为 setOrigin("*")
。 Cross-Origin 政策是关于整个 ULR(准确地说是域),而不是相对路径。
顺便说一句,Spring HTTP 组件集成中有一个 Java DSL 工厂:
@Bean
public IntegrationFlow httpGetTest() {
return IntegrationFlows.from(Http.inboundGateway("/test")
.requestMapping(r -> r.methods(HttpMethod.GET))
.crossOrigin(cors -> cors.origin("*"))
.headerMapper(headerMapper()))
.channel("http.test.channel")
.handle("testEndpoint", "hello")
.get();
}
我有一个 reactjs 前端,它基于 spring-集成向我的 api 发送请求。
我遇到的问题是,我不知道如何在我的网关中绑定 CORS 功能。
我试过这样的东西
@Bean
public CrossOrigin cors(){
CrossOrigin c = new CrossOrigin();
c.setOrigin("/**");
return c;
}
@Bean
public IntegrationFlow httpGetTest() {
return IntegrationFlows.from(httpGetGateTest()).channel("http.test.channel").handle("testEndpoint", "hello").get();
}
@Bean
public MessagingGatewaySupport httpGetGateTest() {
HttpRequestHandlingMessagingGateway handler = new HttpRequestHandlingMessagingGateway();
handler.setRequestMapping(createMapping(new HttpMethod[]{HttpMethod.GET}, "/test"));
handler.setCrossOrigin(cors());
handler.setHeaderMapper(headerMapper());
return handler;
}
要求:
axios.get('http://localhost:8080/test')
.then(res=>{console.log(res)})
我的端点returns"Hello World"
Failed to load resource: the server responded with a status of 415 ()
Access to XMLHttpRequest at 'http://localhost:8080/test' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
首先,请确保您的客户端确实发送了 Origin
HTTP 请求 header。
否则 CORS 过滤不会应用于请求:https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
尽管听起来好像它就在那里:from origin 'http://localhost:3000'
.
考虑将您的 setOrigin("/**")
更改为 setOrigin("*")
。 Cross-Origin 政策是关于整个 ULR(准确地说是域),而不是相对路径。
顺便说一句,Spring HTTP 组件集成中有一个 Java DSL 工厂:
@Bean
public IntegrationFlow httpGetTest() {
return IntegrationFlows.from(Http.inboundGateway("/test")
.requestMapping(r -> r.methods(HttpMethod.GET))
.crossOrigin(cors -> cors.origin("*"))
.headerMapper(headerMapper()))
.channel("http.test.channel")
.handle("testEndpoint", "hello")
.get();
}