Java 使用Wildfly Swarm CORSFilter的REST API
我已经用Wildfly Swarm开发了REST API,我想介绍CORS过滤器,我的要求是所有的头/值都应该可以在外部资源中配置 我已经实现了CORSFilter,但是使用了硬编码的头值,但是现在我希望它可以为生产环境配置Java 使用Wildfly Swarm CORSFilter的REST API,java,cors,jax-rs,wildfly-10,wildfly-swarm,Java,Cors,Jax Rs,Wildfly 10,Wildfly Swarm,我已经用Wildfly Swarm开发了REST API,我想介绍CORS过滤器,我的要求是所有的头/值都应该可以在外部资源中配置 我已经实现了CORSFilter,但是使用了硬编码的头值,但是现在我希望它可以为生产环境配置 有人能指引我吗 我使用属性文件来解决这个问题 我有以下文件 src/main/resources/cors.properties src/main/resources/cors.stage.properties src/main/resources/cors.prod.p
有人能指引我吗 我使用属性文件来解决这个问题 我有以下文件
- src/main/resources/cors.properties
- src/main/resources/cors.stage.properties
- src/main/resources/cors.prod.properties
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete file="${project.build.outputDirectory}/cors.properties"/>
<copy file="src/main/resources/cors.prod.properties"
tofile="${project.build.outputDirectory}/cors.properties"/>
<delete file="${project.build.outputDirectory}/cors.stage.properties"/>
<delete file="${project.build.outputDirectory}/cors.prod.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
我使用属性文件来解决这个问题 我有以下文件
- src/main/resources/cors.properties
- src/main/resources/cors.stage.properties
- src/main/resources/cors.prod.properties
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete file="${project.build.outputDirectory}/cors.properties"/>
<copy file="src/main/resources/cors.prod.properties"
tofile="${project.build.outputDirectory}/cors.properties"/>
<delete file="${project.build.outputDirectory}/cors.stage.properties"/>
<delete file="${project.build.outputDirectory}/cors.prod.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
您可以使用project-.yml根据配置文件更改值(如默认值、生产…)
WRT CORSFilter,您可以使用@ConfigurationValue在yml中注入值
import org.wildfly.swarm.spi.runtime.annotations.ConfigurationValue;
@ApplicationScoped
@Provider
public class CORSFilter implements ContainerResponseFilter {
@Inject @ConfigurationValue("access-control-max-age")
private int accessControlMaxAge;
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext throws IOException {
responseContext.getHeaders().add(
"Access-Control-Max-Age",
accessControlMaxAge // Injected value
);
// other headers ...
}
}
或者,您可以使用带yml的下拖过滤器,而不是CORSFilter
swarm:
undertow:
filter-configuration:
response-headers:
access-control-max-age:
header-name: Access-Control-Max-Age
header-value: -1
# other headers configuration
servers:
default-server:
hosts:
default-host:
filter-refs:
access-control-max-age:
priority: 1
# other filter refs
我创建了一个具有两种方式的示例 您可以使用project-.yml根据配置文件更改值(如默认值、生产…) WRT CORSFilter,您可以使用@ConfigurationValue在yml中注入值
import org.wildfly.swarm.spi.runtime.annotations.ConfigurationValue;
@ApplicationScoped
@Provider
public class CORSFilter implements ContainerResponseFilter {
@Inject @ConfigurationValue("access-control-max-age")
private int accessControlMaxAge;
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext throws IOException {
responseContext.getHeaders().add(
"Access-Control-Max-Age",
accessControlMaxAge // Injected value
);
// other headers ...
}
}
或者,您可以使用带yml的下拖过滤器,而不是CORSFilter
swarm:
undertow:
filter-configuration:
response-headers:
access-control-max-age:
header-name: Access-Control-Max-Age
header-value: -1
# other headers configuration
servers:
default-server:
hosts:
default-host:
filter-refs:
access-control-max-age:
priority: 1
# other filter refs
我创建了一个具有两种方式的示例
我认为我们目前没有办法在project-defaults.yml配置中设置CORS标题。你能提出一个问题吗?我认为我们目前没有办法在project-defaults.yml配置中设置CORS头。你能提出一个问题吗?