【问题标题】:REST APIs With Wildfly Swarm CORSFilter带有 Wildfly Swarm CORSFilter 的 REST API
【发布时间】:2017-07-28 14:28:39
【问题描述】:

我已经使用 Wildfly Swarm 开发了 REST API,我想引入 CORS 过滤器,我的要求是所有标头/值都应该可以在外部资源中配置。

我已经实现了 CORSFilter 但带有硬编码的标头值,但现在我希望它可配置用于生产环境。

有人可以指导我吗?

【问题讨论】:

  • 我认为我们目前没有办法在我们的 project-defaults.yml 配置中设置 CORS 标头。你能提出一个问题吗? issues.jboss.org/browse/SWARM

标签: java cors jax-rs wildfly-10 wildfly-swarm


【解决方案1】:

您可以使用 project-.yml 来更改取决于配置文件的值(如默认值、生产...)。

https://reference.wildfly-swarm.io/v/2017.3.2/configuration.html#_using_yaml

WRT CORSFilter,你可以在 yml 中使用 @ConfigurationValue 注入值。

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 的 Undertow 过滤器而不是 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

我已经创建了一个具有两种方式的示例。

https://github.com/emag-wildfly-swarm-sandbox/wildfly-swarm-cors-filter-demo

【讨论】:

    【解决方案2】:

    我使用属性文件来解决这个问题。

    我有以下文件

    • src/main/resources/cors.properties
    • src/main/resources/cors.stage.properties
    • src/main/resources/cors.prod.properties

    比我使用 maven-antrun-plugin 根据所选的 maven 配置文件使用正确的属性文件。

    <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>
    

    完整的maven配置请查看https://maven.apache.org/guides/mini/guide-building-for-different-environments.html

    然后您可以从资源中加载属性,遍历它们并添加标题

    Properties properties = new Properties();
    InputStream in = getClass().getClassLoader().getResourceAsStream("cors.properties");
    properties.load(in);
    in.close();
    
    for (String name : properties.stringPropertyNames()) {
        addHeader(name, properties.getProperty(name));
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      • 2014-04-25
      相关资源
      最近更新 更多