2

我正在尝试从 2.4 迁移到玩 2.5。我在过滤器代码中遇到问题。下面是我的过滤器代码

public class Filters implements HttpFilters {

    private final SecurityFilter securityFilter;

    @Inject
    public Filters(SecurityFilter securityFilter) {
        this.securityFilter = securityFilter;
    }

    @Override
    public EssentialFilter[] filters() {
        return new EssentialFilter[] {securityFilter.asJava()};
    }
}

根据文档https://www.playframework.com/documentation/2.5.x/JavaHttpFilters

asJava() 方法可用,并在 Eclipse 中完美编译 2.5 jar。但是当我通过 activator ui 或 activator dist 运行时,上面的代码会失败

我已经用下面的 addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.1") 更新了 plugins.sbt

但它仍然失败。还有什么应该改变的?或者是否有任何其他配置要更改?

4

1 回答 1

0

当我将我的应用程序从 2.4 迁移到 2.5 时,我遇到了同样的问题,我以这种方式解决了:

我将过滤器代码更改为:

import play.filters.cors.CORSFilter;
import play.http.DefaultHttpFilters;

import javax.inject.Inject;

public class Filters extends DefaultHttpFilters {

@Inject public Filters(CORSFilter corsFilter) {
    super(corsFilter);
}

并按照文档中的步骤操作:启用 CORS 过滤器

添加 build.sbt

libraryDependencies +=过滤器

并在 application.conf 中配置 cors:

filters{
# CORS filter configuration
cors {

  # The path prefixes to filter.
  pathPrefixes = ["/json/rest/api"]

  # The allowed origins. If null, all origins are allowed.
  allowedOrigins = null

  # The allowed HTTP methods. If null, all methods are allowed
  allowedHttpMethods = ["POST","GET"]

  # The allowed HTTP headers. If null, all headers are allowed.
  allowedHttpHeaders = null

  # The exposed headers
  exposedHeaders = []

  # Whether to support credentials
  supportsCredentials = true

  # The maximum amount of time the CORS meta data should be cached by the client
  preflightMaxAge = 1 hour
}

随意问任何问题。

于 2016-11-08T17:58:09.570 回答