当用户无权访问它们时,我需要发布任何 Micronaut 控制器的响应并消除响应正文中的项目。
在一个阻塞的世界中,我会像这样实现它
protected MutableHttpResponse<?> doFilterOnce(HttpRequest<?> request, ServerFilterChain chain) {
// If Micronaut Security rejected the request simpy do nothing
if (request.getAttribute(SecurityFilter.REJECTION).isPresent()) {
log.debug("Request was previously rejected. Not going to contact PDP");
return chain.proceed(request);
}
HttpMethod method = request.getMethod();
if (method.equals(GET) || method.equals(HEAD)) {
MutableHttpResponse<?> response = chain.proceed(request);
if (response.getBody().isPresent()) {
// iterate through the body
Object theBody = response.getBody().get();
if (theBody instanceof Collection) {
Collection<?> iterable = (Iterable<?>) theBody;
// select all elements that are rejected. This is a blocking call.
List<?> collect = iterable.stream().filter(item -> mySecService.isAllowed(item) == false).collect(Collectors.toList());
// remove them
iterable.removeAll(collect);
// reset the body
response.body(iterable);
}
}
} else {
return chain.proceed(request)
}
return response;
}
Micronaut 指出
过滤器在事件循环中执行,因此阻塞操作必须卸载到另一个线程池。
因此在现实世界中它需要 mit 返回
- 可流动的
- 以反应的方式实现上面的代码
这是我到目前为止所做的。
if (method.equals(GET) || method.equals(HEAD)) {
// post process
return Flowable.fromPublisher(chain.proceed(request))
.doNext(response -> {
Optional<?> body = response.getBody();
if (body.isPresent()) {
// how can I continue here an process the response body collection?
}
});
}
有人可以给我一个提示如何继续处理响应正文,进行安全检查,删除项目并重置新正文吗?