Micronaut CORS设置无法从hls.js XmlHttpRequest中正常使用

嗨,我已经创建了一个服务于master.m3u8文件的api接口,该文件内部链接到playlist.m3u8中的其他http://path to resource pattern文件

我的API开发框架是micronaut

我的application.yml设置如下:

---
micronaut:
  application:
    name: kuchipuchipoo
  server:
    cors:
      enabled: true
      configurations:
        web:
          allowCredentials: true
          allowedMethods:
            - OPTIONS
            - POST
            - PUT
            - GET
          exposedHeaders:
            - access-control-allow-origin
            - access-Control-Allow-Headers
            - access-Control-Request-Method
            - access-Control-Request-Headers
            - Origin
            - Cache-Control
            - Content-Type
            - Authorization
    port: 9093
    max-request-size: 256MB
    multipart:
      max-file-size: 256MB
---

当我尝试从hls.js发出此请求(即play()命令)时,出现以下错误:

access to XMLHttpRequest at 'http://localhost:9093/path?params' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'access-control-allow-origin' header is present on the requested resource.

AFAIR,我仍然在应用程序配置中添加了access-control-allow-origin,但似乎没有任何作用。

我的客户端应用程序的角度为7+,这两个都是在不同端口上运行的单独应用程序,请注意。

已编辑

在服务器实现下面很难解决:

@Get("/path/to/playlist.m3u8")
    @Produces("application/x-mpegURL")
    @Secured(SecurityRule.IS_ANONYMOUS)
    public HttpResponse<File> getResolutionFileNew(vars id) {
        MutableHttpResponse<File> ok = HttpResponse.ok(fileDeliveryManager.getResolutionsFile(id));
        return ok.header("access-control-allow-origin","http://localhost:4200");
    }

    @Get("/path/to/file{index}.ts")
    @Produces("video/MP2T")
    @Secured(SecurityRule.IS_ANONYMOUS)
    public HttpResponse<File> getFileChunk(vars id) {
        final File file = fileDeliveryManager.getchunkFile(file0.ts,id);
        log.debug("Transporting ts file {}",file);
        MutableHttpResponse<File> ok = HttpResponse.ok(file);
        return ok;
    }

如果我不添加ok.header("access-control-allow-origin","http://localhost:4200");,那就行不通了。

我在这里完全感到困惑,似乎application.yml中的cors设置什么都不做。 以前,我使用过Spring Boot api框架,我们不必以这种方式转储标头,我在这里肯定做错了事,但无法弄清楚。任何帮助都会帮助我。

tmdgzz 回答:Micronaut CORS设置无法从hls.js XmlHttpRequest中正常使用

尝试设置micronaut.server.cors.single-header: true。某些浏览器不遵循具有多个标头的标准。

本文链接:https://www.f2er.com/3016181.html

大家都在问