Grails 3 为 application.yml 中的多个映射配置 CORS

Grails 3 configuring CORS for multiple mappings in application.yml

我已经尝试按照此处的官方文档指南进行操作 http://docs.grails.org/3.3.11/guide/single.html#cors 但奇怪的是我发现只有第一个映射会被应用。

他们的例子:

grails:
    cors:
        enabled: true
        mappings:
            /api/**: inherit

我假设会起作用但不起作用(仅应用第一个映射):

    grails:
        cors:
            enabled: true
            mappings:
                /api/**: inherit
                /api2/**: inherit
                ..

我一定是遗漏了一些非常简单的东西,因为我惊讶地发现没有关于像这样的多个映射的文档或问题。对此有什么想法吗?

是的,这也让我丧命。我们在 BeAPI Grails 插件(我维护的)中解决了这个问题,方法是将映射添加到 beapi_api.yml 配置文件:

corsInterceptor:
    includeEnvironments: ['development','test','production']
    excludeEnvironments: []
    networkGroups:
        open: ['http://localhost','http://localhost:8080','http://127.0.0.1','http://test.nosegrind.net','http://test.nosegrind.net:8080']
        public: ['http://localhost','http://localhost:8080','http://127.0.0.1','http://test.nosegrind.net','http://test.nosegrind.net:8080']
        private: ['http://localhost','http://localhost:8080','http://127.0.0.1','http://test.nosegrind.net','http://test.nosegrind.net:8080']

检查每个请求以查看它属于什么'networkGroup'(在同一个配置文件中声明):

networkGroups: ['open','public','private']
networkRoles:
    open: ['ROLE_ADMIN','ROLE_ANONYMOUS','ROLE_USER']
    public: ['ROLE_ADMIN','ROLE_USER']
    private: ['ROLE_ADMIN']

这允许在进行 CORS 检查时 GROUP/ROLE 检查以及 FQDN/IP 检查前端。

所以您所要做的就是将您的 FQDN/IP 添加到适当的 'corsInterceptor.networkGroup'