Add remove json attributes filter (#2742)

* Add RemoveJsonAttributesFilterFactory
Takes a collection of attribute names to search for to remove from a JSON response, an optional last parameter from the list can be a boolean to remove the attributes just at root level or recursively

* Add docs for RemoveJsonAttributes filter

* Extract a single instance of Json mapper

* Overload boolean parameter to avoid parsing a string
This commit is contained in:
Marta Medio
2022-10-05 17:39:48 +02:00
committed by GitHub
parent 253eec53a8
commit a487e64bfd
5 changed files with 335 additions and 0 deletions

View File

@@ -1118,6 +1118,51 @@ spring:
This will send a status 302 with a `Location:https://acme.org` header to perform a redirect.
=== `RemoveJsonAttributesResponseBody` `GatewayFilter` Factory
The `RemoveJsonAttributesResponseBody` `GatewayFilter` factory takes a collection of `attribute names` to search for, an optional last parameter from the list can be a boolean to remove the attributes just at root level (that's the default value if not present at the end of the parameter configuration, `false`) or recursively (`true`).
It provides a convenient method to apply a transformation to JSON body content by deleting attributes from it.
The following example configures an `RemoveJsonAttributesResponseBody` `GatewayFilter`:
.application.yml
====
[source,yaml]
----
spring:
cloud:
gateway:
routes:
- id: removejsonattributes_route
uri: https://example.org
filters:
- RemoveJsonAttributesResponseBody=id,color
----
====
This removes attributes "id" and "color" from the JSON content body at root level.
The following example configures an `RemoveJsonAttributesResponseBody` `GatewayFilter` that uses the optional last parameter:
.application.yml
====
[source,yaml]
----
spring:
cloud:
gateway:
routes:
- id: removejsonattributes_recursively_route
uri: https://example.org
predicates:
- Path=/red/{segment}
filters:
- RemoveJsonAttributesResponseBody=id,color,true
----
====
This removes attributes "id" and "color" from the JSON content body at any level.
=== The `RemoveRequestHeader` GatewayFilter Factory
The `RemoveRequestHeader` `GatewayFilter` factory takes a `name` parameter.

View File

@@ -79,6 +79,7 @@ import org.springframework.cloud.gateway.filter.factory.MapRequestHeaderGatewayF
import org.springframework.cloud.gateway.filter.factory.PrefixPathGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.PreserveHostHeaderGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.RedirectToGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.RemoveJsonAttributesResponseBodyGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.RemoveRequestHeaderGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.RemoveRequestParameterGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.RemoveResponseHeaderGatewayFilterFactory;
@@ -549,6 +550,15 @@ public class GatewayAutoConfiguration {
return new RedirectToGatewayFilterFactory();
}
@Bean
@ConditionalOnEnabledFilter
public RemoveJsonAttributesResponseBodyGatewayFilterFactory removeJsonAttributesResponseBodyGatewayFilterFactory(
ServerCodecConfigurer codecConfigurer, Set<MessageBodyDecoder> bodyDecoders,
Set<MessageBodyEncoder> bodyEncoders) {
return new RemoveJsonAttributesResponseBodyGatewayFilterFactory(
new ModifyResponseBodyGatewayFilterFactory(codecConfigurer.getReaders(), bodyDecoders, bodyEncoders));
}
@Bean
@ConditionalOnEnabledFilter
public RemoveRequestHeaderGatewayFilterFactory removeRequestHeaderGatewayFilterFactory() {

View File

@@ -0,0 +1,147 @@
/*
* Copyright 2013-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.gateway.filter.factory;
import java.util.Arrays;
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import reactor.core.publisher.Mono;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.rewrite.ModifyResponseBodyGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.rewrite.RewriteFunction;
import org.springframework.core.style.ToStringCreator;
import org.springframework.http.MediaType;
/**
* @author Marta Medio
*/
public class RemoveJsonAttributesResponseBodyGatewayFilterFactory extends
AbstractGatewayFilterFactory<RemoveJsonAttributesResponseBodyGatewayFilterFactory.FieldListConfiguration> {
public RemoveJsonAttributesResponseBodyGatewayFilterFactory(
ModifyResponseBodyGatewayFilterFactory modifyResponseBodyGatewayFilterFactory) {
this.modifyResponseBodyGatewayFilterFactory = modifyResponseBodyGatewayFilterFactory;
}
@Override
public ShortcutType shortcutType() {
return ShortcutType.GATHER_LIST_TAIL_FLAG;
}
@Override
public List<String> shortcutFieldOrder() {
return Arrays.asList("fieldList", "deleteRecursively");
}
@Override
public FieldListConfiguration newConfig() {
return new FieldListConfiguration();
}
@Override
public Class<FieldListConfiguration> getConfigClass() {
return FieldListConfiguration.class;
}
@Override
public GatewayFilter apply(FieldListConfiguration config) {
ModifyResponseBodyGatewayFilterFactory.Config modifyResponseBodyConfig = new ModifyResponseBodyGatewayFilterFactory.Config();
modifyResponseBodyConfig.setInClass(String.class);
modifyResponseBodyConfig.setOutClass(String.class);
RewriteFunction<String, String> rewriteFunction = (exchange, body) -> {
if (MediaType.APPLICATION_JSON.isCompatibleWith(exchange.getResponse().getHeaders().getContentType())) {
try {
JsonNode jsonBodyContent = mapper.readValue(body, JsonNode.class);
removeJsonAttribute(jsonBodyContent, config.getFieldList(), config.isDeleteRecursively());
body = mapper.writeValueAsString(jsonBodyContent);
}
catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
return Mono.just(body);
};
modifyResponseBodyConfig.setRewriteFunction(rewriteFunction);
return modifyResponseBodyGatewayFilterFactory.apply(modifyResponseBodyConfig);
}
private final ModifyResponseBodyGatewayFilterFactory modifyResponseBodyGatewayFilterFactory;
private ObjectMapper mapper = new ObjectMapper();
private void removeJsonAttribute(JsonNode jsonBodyContent, List<String> fieldsToRemove, boolean deleteRecursively) {
if (deleteRecursively) {
for (JsonNode jsonNode : jsonBodyContent) {
if (jsonNode instanceof ObjectNode) {
((ObjectNode) jsonNode).remove(fieldsToRemove);
removeJsonAttribute(jsonNode, fieldsToRemove, true);
}
if (jsonNode instanceof ArrayNode) {
for (JsonNode node : jsonNode) {
removeJsonAttribute(node, fieldsToRemove, true);
}
}
}
}
if (jsonBodyContent instanceof ObjectNode) {
((ObjectNode) jsonBodyContent).remove(fieldsToRemove);
}
}
public static class FieldListConfiguration {
private List<String> fieldList;
private boolean deleteRecursively;
public boolean isDeleteRecursively() {
return deleteRecursively;
}
public FieldListConfiguration setDeleteRecursively(boolean deleteRecursively) {
this.deleteRecursively = deleteRecursively;
return this;
}
List<String> getFieldList() {
return fieldList;
}
public FieldListConfiguration setFieldList(List<String> fieldList) {
this.fieldList = fieldList;
return this;
}
@Override
public String toString() {
return new ToStringCreator(this).append("fieldList", fieldList)
.append("deleteRecursively", deleteRecursively).toString();
}
}
}

View File

@@ -51,6 +51,7 @@ import org.springframework.cloud.gateway.filter.factory.MapRequestHeaderGatewayF
import org.springframework.cloud.gateway.filter.factory.PrefixPathGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.PreserveHostHeaderGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.RedirectToGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.RemoveJsonAttributesResponseBodyGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.RemoveRequestHeaderGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.RemoveRequestParameterGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.RemoveResponseHeaderGatewayFilterFactory;
@@ -464,6 +465,19 @@ public class GatewayFilterSpec extends UriSpec {
}
}
/**
* A filter that can be used to modify the response body.
* @param attributes list of attributes to remove separated by commas, an optional
* last parameter from the list can be a boolean to remove the attributes just at root
* level (false) o recursively (true)
* @return a {@link GatewayFilterSpec} that can be used to apply additional filters
*/
public GatewayFilterSpec removeJsonAttributes(boolean deleteRecursively, String... attributes) {
return filter(getBean(RemoveJsonAttributesResponseBodyGatewayFilterFactory.class)
.apply(c -> c.setFieldList(Arrays.asList(attributes)).setDeleteRecursively(deleteRecursively)));
}
/**
* A filter that will remove a request header before the request is routed by the
* Gateway.

View File

@@ -0,0 +1,119 @@
/*
* Copyright 2013-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.gateway.filter.factory;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.gateway.test.BaseWebClientTests;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.gateway.test.TestUtils.getMap;
/**
* @author Marta Medio
*/
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class RemoveJsonAttributesResponseBodyGatewayFilterFactoryTests extends BaseWebClientTests {
@Test
public void removeJsonAttributeRootWorks() {
testClient.post().uri("/post").header("Host", "www.removejsonattributes.org")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).header("foo", "test")
.header("bar", "test").exchange().expectStatus().isOk().expectBody(Map.class).consumeWith(result -> {
Map<?, ?> response = result.getResponseBody();
assertThat(response).isNotNull();
String responseBody = (String) response.get("data");
assertThat(responseBody).isNull();
Map<String, Object> headers = getMap(response, "headers");
assertThat(headers).containsKey("user-agent");
});
}
@Test
public void removeJsonAttributeRecursivelyWorks() {
testClient.post().uri("/post").header("Host", "www.removejsonattributesrecursively.org")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).header("foo", "test")
.header("bar", "test").exchange().expectStatus().isOk().expectBody(Map.class).consumeWith(result -> {
Map<?, ?> response = result.getResponseBody();
assertThat(response).isNotNull();
Map<String, Object> headers = getMap(response, "headers");
assertThat(headers).doesNotContainKey("foo");
assertThat(headers).containsEntry("bar", "test");
});
}
@Test
public void removeJsonAttributeNoMatchesWorks() {
testClient.post().uri("/post").header("Host", "www.removejsonattributesnomatches.org")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).exchange().expectStatus().isOk()
.expectBody(Map.class).consumeWith(result -> {
Map<?, ?> response = result.getResponseBody();
assertThat(response).isNotNull();
Map<String, Object> headers = getMap(response, "headers");
assertThat(headers).isNotNull();
assertThat(headers).containsEntry(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
});
}
@EnableAutoConfiguration
@SpringBootConfiguration
@Import(DefaultTestConfig.class)
public static class TestConfig {
@Value("${test.uri}")
String uri;
@Bean
public RouteLocator testRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("remove_json_attributes_root_level_java_test",
r -> r.path("/post").and().host("{sub}.removejsonattributes.org")
.filters(f -> f.removeJsonAttributes(false, "data", "foo")).uri(uri))
.route("remove_json_attributes_recursively_java_test",
r -> r.path("/post").and().host("{sub}.removejsonattributesrecursively.org")
.filters(f -> f.removeJsonAttributes(true, "foo")).uri(uri))
.route("remove_json_attributes_no_matches_java_test",
r -> r.path("/post").and().host("{sub}.removejsonattributesnomatches.org")
.filters(f -> f.removeJsonAttributes(false, "test")).uri(uri))
.build();
}
}
}