cors - 如何在 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。
解决方案
不必手动编辑捆绑清单,因为它是由捆绑插件生成的。您当前正在构建一个普通的 jar,而不是一个包。请将以下内容添加到version
标签正下方的 pom 中:
<packaging>container-plugin</packaging>
现在它将被构建为一个包,并且将正确生成清单。
您可以删除vespa-application-maven-plugin
, 因为只需要使用 services.xml 等创建应用程序包。如果您愿意,可以将过滤器类与您可能拥有的任何其他 java 类一起放入主应用程序,并跳过额外的包完全地。
推荐阅读
- macos - 更新到 macOS Mojave 后编译器无法搜索“/usr/local/include”
- javascript - 用可配置的 false 定义的 SinonJS 存根 getter
- sap-gui - 从 SAP 中提取长文本数据到 Excel
- postgresql - 分离 Alfresco 服务器和 postgresql 数据库
- php - 如何优化计数分数的功能?
- git - 如何比较来自两个 Git 分支的两个文件?
- javascript - 粒子函数中的 JavaScript 中的类崩溃
- python - 如何检查列表中的项目是否可以作为另一个列表中的项目找到
- excel - 要对位于列中单元格之间的 excel 列中的单元格求和,请符合特定条件
- dart - Flutter 应用程序冻结并且无法按预期工作