如何在Grails 3.0.1中启用CORS [英] How to enable CORS in Grails 3.0.1
问题描述
我想在服务器端使用Grails进行跨源通信。我发现的唯一文档是这一个
I would like to do cross origin communication using Grails in server side. The only documentation that I found is this one
https://grails.org/ plugin / cors
但这是一个旧版本的Grails。我发现的其他文档是春天:
but this is for an old version of Grails. The other documentation that I found is for spring:
https://spring.io/guides/gs/rest-service-cors/
所以我添加了文件 SimpleCorsFilter。 groovy
到 init / myproject /
文件夹,但我不知道如何将此组件连接到 resources.groovy
so I added the file SimpleCorsFilter.groovy
to init/myproject/
folder, but I don't know how to wire this component into resources.groovy
推荐答案
具体来说,这里有一些代码。注意拦截器名称必须与你的控制器名称(这里是workRequest)匹配,域需要是你调用的任何东西(这里是localhost:8081),它是你想要的before()方法:
To be specific, here is some code that works. Notice the interceptor name must match your controller name (here, workRequest), the domain needs to be whatever you are calling from (here, localhost:8081) and it is the before() method you want:
package rest
class WorkRequestInterceptor {
boolean before() {
header( "Access-Control-Allow-Origin", "http://localhost:8081" )
header( "Access-Control-Allow-Credentials", "true" )
header( "Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE" )
header( "Access-Control-Max-Age", "3600" )
true
}
boolean after() { true }
}
这篇关于如何在Grails 3.0.1中启用CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!