我有一个reactjs前端,它基于spring-integration向我的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)})
我的端点返回“ 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.
答案 0 :(得分:1)
首先,请确保您的客户端确实发送了 //This my code for delete
$listening = Listening::find($id);
if ($listening->audio !== $request->audio) {
unlink(public_path('listening/admin/'.$listening->audio));
}
HTTP请求标头。
否则,CORS过滤将不会应用于请求:https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
尽管听起来好像仍然存在:Origin
。
请考虑将您的from origin 'http://localhost:3000'
更改为setOrigin("/**")
。跨域策略是关于整个ULR(准确地说是域),而不是相对路径。
顺便说一句,Spring Integration中有一个Java DSL工厂用于HTTP组件:
setOrigin("*")