Play Framework 2.5.12 - 在 application.conf 中设置多个过滤器
Play Framework 2.5.12 - set multiple filter in application.conf
我正在使用 Play Framework 2.5.12(Java 版本)。
当我声明过滤器 (Filters1) 并在 application.conf:
中设置以下配置时
play.http.filters = my.filter.path.Filters1
它工作正常。
但是我有两个过滤器:
- Filters1 扩展了 DefaultHttpFilters class
- Filters2 扩展过滤器 class
在 Play Framework 2.6 中,解决方案是:
play.http.filters += my.filter.path.Filters1
play.http.filters += my.filter.path.Filters2
我试过了,但它在 Play Framework 2.5 中不起作用。
你的解决方案是什么?
最终我无法在application.conf中处理这个问题。
解决方案:
您必须将过滤器作为参数传递到 Filters1 的构造函数中,并将其设置为 EssentialFilters。
这是我的过滤器 1 class :
import com.rh.ot.web.core.filters.CustomFilter;
import play.Environment;
import play.filters.gzip.GzipFilter;
import play.filters.headers.SecurityHeadersFilter;
import play.http.DefaultHttpFilters;
import play.mvc.EssentialFilter;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class Filters1 extends DefaultHttpFilters {
private final Environment env;
private EssentialFilter[] filters;
@Inject
public Filters(Environment env, GzipFilter gzipFilter, SecurityHeadersFilter securityHeadersFilter, Filters2 filters2) {
super(securityHeadersFilter);
this.env = env;
filters = new EssentialFilter[]{gzipFilter.asJava(), filters2.asJava()};
}
public EssentialFilter[] filters() {
return filters;
}
}
和过滤器 2 class:
import akka.util.ByteString;
import play.libs.streams.Accumulator;
import play.mvc.EssentialAction;
import play.mvc.EssentialFilter;
import play.mvc.Result;
import javax.inject.Inject;
import java.util.concurrent.Executor;
public class Filters2 extends EssentialFilter {
private final Executor executor;
@Inject
public Filters2(Executor executor) {
super();
this.executor = executor;
}
@Override
public EssentialAction apply(EssentialAction next) {
return EssentialAction.of(request -> {
request.headers().put("Strict-Transport-Security", new String[]{"max-age=31536000; includeSubDomains"});
Accumulator<ByteString, Result> accumulator = next.apply(request);
return accumulator.map(result -> result, executor);
});
}
}
我正在使用 Play Framework 2.5.12(Java 版本)。 当我声明过滤器 (Filters1) 并在 application.conf:
中设置以下配置时 play.http.filters = my.filter.path.Filters1
它工作正常。
但是我有两个过滤器:
- Filters1 扩展了 DefaultHttpFilters class
- Filters2 扩展过滤器 class
在 Play Framework 2.6 中,解决方案是:
play.http.filters += my.filter.path.Filters1
play.http.filters += my.filter.path.Filters2
我试过了,但它在 Play Framework 2.5 中不起作用。 你的解决方案是什么?
最终我无法在application.conf中处理这个问题。
解决方案:
您必须将过滤器作为参数传递到 Filters1 的构造函数中,并将其设置为 EssentialFilters。
这是我的过滤器 1 class :
import com.rh.ot.web.core.filters.CustomFilter;
import play.Environment;
import play.filters.gzip.GzipFilter;
import play.filters.headers.SecurityHeadersFilter;
import play.http.DefaultHttpFilters;
import play.mvc.EssentialFilter;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class Filters1 extends DefaultHttpFilters {
private final Environment env;
private EssentialFilter[] filters;
@Inject
public Filters(Environment env, GzipFilter gzipFilter, SecurityHeadersFilter securityHeadersFilter, Filters2 filters2) {
super(securityHeadersFilter);
this.env = env;
filters = new EssentialFilter[]{gzipFilter.asJava(), filters2.asJava()};
}
public EssentialFilter[] filters() {
return filters;
}
}
和过滤器 2 class:
import akka.util.ByteString;
import play.libs.streams.Accumulator;
import play.mvc.EssentialAction;
import play.mvc.EssentialFilter;
import play.mvc.Result;
import javax.inject.Inject;
import java.util.concurrent.Executor;
public class Filters2 extends EssentialFilter {
private final Executor executor;
@Inject
public Filters2(Executor executor) {
super();
this.executor = executor;
}
@Override
public EssentialAction apply(EssentialAction next) {
return EssentialAction.of(request -> {
request.headers().put("Strict-Transport-Security", new String[]{"max-age=31536000; includeSubDomains"});
Accumulator<ByteString, Result> accumulator = next.apply(request);
return accumulator.map(result -> result, executor);
});
}
}