首页 > 解决方案 > 如何在 Vespa 中添加自定义过滤器包以启用 CORS?

问题描述

我已经关注了在 vespa 中启用 CORS的问题,并使用 RequestFilter 和 ResponseFilter 编写并构建了一个新的自定义“过滤器捆绑”包,该包添加了启用 CORS 的标头。这是我用于构建捆绑包的 pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.yahoo.bundle</groupId>
  <artifactId>filter-bundle</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>com.yahoo.vespa</groupId>
      <artifactId>container</artifactId>
      <version>6.297.80</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>

      <plugin>
        <groupId>com.yahoo.vespa</groupId>
        <artifactId>vespa-application-maven-plugin</artifactId>
        <!-- Zip the application package -->
        <version>6.297.80</version>
        <executions>
          <execution>
            <goals>
              <goal>packageApplication</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>com.yahoo.vespa</groupId>
        <artifactId>bundle-plugin</artifactId>
        <version>6.297.80</version>
        <extensions>true</extensions>
        <configuration>
          <attachBundleArtifact>true</attachBundleArtifact>
          <bundleSymbolicName>filter-bundle</bundleSymbolicName>
          <bundleVersion>1.0.2</bundleVersion>
        </configuration>
      </plugin>

    </plugins>
  </build>



</project>

我将构建的 jar 包添加到我的“主”应用程序包的 /components 目录中,并按照链接https://docs.vespa.ai/documentation/jdisc/http-server-在我的 services.xml 文件中进行了建议的更改and-filters.html 我将以下 HTTP 标记添加到我的 services.xml 文件中。

<http>
  <filtering>
    <filter id='request-filter' class='com.yahoo.bundle.MyRequestFilter' bundle="filter-bundle" />
    <filter id='response-filter' class='com.yahoo.bundle.MyResponseFilter' bundle="filter-bundle" />

    <request-chain id='request-chain'>
      <filter id='request-filter' />
      <binding>http://*/*</binding>
    </request-chain>

    <response-chain id='response-chain'>
      <filter id='response-filter' />
      <binding>http://*/*</binding>
    </response-chain>
  </filtering>
  <server port="8080" id="main-server" />
</http>

这里的“MyRequestFilter”和“MyResponseFilter”是我的过滤器包中的类名(在应用程序/组件目录中的内置过滤器包 jar 中)。

遵循这些步骤给了我一个错误,即过滤器捆绑包的 jar 文件中的 MANIFEST.MF 文件中缺少一些标头,例如“Bundle-SymbolicName”、“Bundle-ManifestVersion”。所以我编辑了 MANIFEST.MF 文件以在https://docs.vespa.ai/documentation/bundle-plugin.html Edit-1: 添加“container-plugin”行正确生成 MANIFEST.MF 文件之后添加所需的标题

但是,在使用 jar 文件中的上述更改成功构建我的应用程序后,我仍然无法在对 Vespa 的请求或响应中看到添加的标头,并且仍然收到一个错误,即在 Vespa 上禁用了 CORS。

标签: corsvespa

解决方案


不必手动编辑捆绑清单,因为它是由捆绑插件生成的。您当前正在构建一个普通的 jar,而不是一个包。请将以下内容添加到version标签正下方的 pom 中:

<packaging>container-plugin</packaging>

现在它将被构建为一个包,并且将正确生成清单。

您可以删除vespa-application-maven-plugin, 因为只需要使用 services.xml 等创建应用程序包。如果您愿意,可以将过滤器类与您可能拥有的任何其他 java 类一起放入主应用程序,并跳过额外的包完全地。


推荐阅读