2

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

マイクロノートは次のように述べています。

フィルタはイベント ループで実行されるため、ブロッキング操作は別のスレッド プールにオフロードする必要があります。

したがって、現実の世界では、 mit を返す必要があります

  1. 流動性
  2. 上記のコードをリアクティブな方法で実装する

これは私がこれまで行ってきたことです。

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?
                }
            });
}

誰かが応答本文の処理を続行し、セキュリティ チェックを行い、アイテムを削除して新しい本文をリセットする方法を教えてもらえますか?

4

1 に答える 1