[英]How to drop HTTP requests in a Java Play! filter?
I have a filter (coded in Java) that processes every HTTP request made to my app. 我有一个过滤器(用Java编码)可以处理对我的应用程序发出的每个HTTP请求。 I want this filter to look at the value of a specific header, and based on that value, drop (ie eventually return a 404 error) this current HTTP request. 我希望此过滤器查看特定标头的值,然后基于该值删除此当前HTTP请求(即最终返回404错误)。
Is it possible to do this in Play? 是否可以在Play中执行此操作?
Thanks, 谢谢,
This is fairly straight-forward. 这很简单。 First of all, read and understand the section on filters in the documentation . 首先,阅读并理解文档中有关过滤器的部分。
The implementation of your filter then looks like this: 过滤器的实现如下所示:
import akka.stream.Materializer;
import play.mvc.Filter;
import play.mvc.Http;
public class HeaderFilter extends Filter {
@Inject
public HeaderFilter(final Materializer mat) {
super(mat);
}
@Override
public CompletionStage<Result> apply(final Function<Http.RequestHeader, CompletionStage<Result>> next,
final Http.RequestHeader requestHeader) {
final String header = requestHeader.getHeader("header name");
if (some test) {
// allow the request to continue
return next.apply(requestHeader);
} else {
// block the request
return CompletableFuture.completedFuture(Results.notFound());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.