如何在Grails 3.0.1中启用CORS

编程入门 行业动态 更新时间:2024-10-18 02:30:04
本文介绍了如何在Grails 3.0.1中启用CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在服务器端使用Grails进行跨源通信。我发现的唯一文档是这一个

I would like to do cross origin communication using Grails in server side. The only documentation that I found is this one

grails/ plugin / cors

但这是一个旧版本的Grails。我发现的其他文档是春天:

but this is for an old version of Grails. The other documentation that I found is for spring:

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", "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

本文发布于:2023-11-24 20:39:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626742.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Grails   CORS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!