Lui*_*gas 18 grails cors grails-3.0
我想在服务器端使用Grails进行跨源通信.我找到的唯一文档就是这个
https://grails.org/plugin/cors
但这是旧版的Grails.我找到的其他文件是春天:
https://spring.io/guides/gs/rest-service-cors/
所以我添加的文件SimpleCorsFilter.groovy
到init/myproject/
文件夹,但我不知道如何把这个组件成resources.groovy
Rat*_*ata 22
所以,如果你使用grails 3.2.+来到这里,你可以使用默认方式.
转到您的application.yml
并添加:
grails:
cors:
enabled: true
Run Code Online (Sandbox Code Playgroud)
它会添加Access-Control-Allow-Origin '*'
.如果您想要不同的东西,请查看此页面
Dav*_*ker 12
我们使用了一个普通的servlet过滤器和resources.groovy中的条目来解决这个问题:
public class CorsFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest req, HttpServletResponse resp, FilterChain chain)
throws ServletException, IOException {
String origin = req.getHeader("Origin");
boolean options = "OPTIONS".equals(req.getMethod());
if (options) {
if (origin == null) return;
resp.addHeader("Access-Control-Allow-Headers", "origin, authorization, accept, content-type, x-requested-with");
resp.addHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS");
resp.addHeader("Access-Control-Max-Age", "3600");
}
resp.addHeader("Access-Control-Allow-Origin", origin == null ? "*" : origin);
resp.addHeader("Access-Control-Allow-Credentials", "true");
if (!options) chain.doFilter(req, resp);
}
}
Run Code Online (Sandbox Code Playgroud)
resources.groovy:
beans = {
corsFilter(CorsFilter)
}
Run Code Online (Sandbox Code Playgroud)
这适用于使用基本身份验证的CORS请求.我编写了Grails 2.x插件,这似乎比使用Grails 3更容易.
小智 9
具体来说,这里有一些有用的代码.请注意,拦截器名称必须与您的控制器名称(此处为workRequest)匹配,域名需要是您要调用的任何内容(此处为localhost:8081),并且它是您想要的before()方法:
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 }
}
Run Code Online (Sandbox Code Playgroud)
小智 6
当我遇到CORS问题时,我正在使用emberjs框架和Grails 3.0休息应用程序.按照本文中的步骤http://www.greggbolinger.com/rendering-json-in-grails-for-ember-js/帮助我进一步发展.
本文展示了如何使用新的Interceptor创建一个CorsInterceptor类来设置正确的标头.
class CorsInterceptor {
CorsInterceptor() {
matchAll()
}
boolean before() {
header( "Access-Control-Allow-Origin", "http://localhost:4200" )
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 }
}
Run Code Online (Sandbox Code Playgroud)
这对于GET请求按预期工作,但对POST和PUT请求失败.原因是OPTIONS预检请求首先发送到http:// localhost:8080/mycontroller/1234,在我的情况下导致找不到404被返回.
在这个答案的帮助下/sf/answers/2228418601/我将CorsInterceptor类修改为:
class CorsInterceptor {
CorsInterceptor() {
matchAll()
}
boolean before() {
header( "Access-Control-Allow-Origin", "http://localhost:4200" )
boolean options = ("OPTIONS" == request.method)
if (options) {
header( "Access-Control-Allow-Origin", "http://localhost:4200" )
header( "Access-Control-Allow-Credentials", "true" )
header( "Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE")
header( "Access-Control-Max-Age", "3600" )
response.status = 200
}
true
}
boolean after() { true }
}
Run Code Online (Sandbox Code Playgroud)
现在POST和PUT请求正在运行.
归档时间: |
|
查看次数: |
9203 次 |
最近记录: |