|
31 | 31 | import org.springframework.http.HttpHeaders; |
32 | 32 | import org.springframework.http.server.ServletServerHttpRequest; |
33 | 33 | import org.springframework.util.Assert; |
| 34 | +import org.springframework.util.LinkedMultiValueMap; |
| 35 | +import org.springframework.util.MultiValueMap; |
34 | 36 | import org.springframework.util.StringUtils; |
35 | 37 | import org.springframework.web.util.ContentCachingRequestWrapper; |
| 38 | +import org.springframework.web.util.UriComponentsBuilder; |
36 | 39 | import org.springframework.web.util.WebUtils; |
37 | 40 |
|
38 | 41 | /** |
@@ -99,6 +102,8 @@ public abstract class AbstractRequestLoggingFilter extends OncePerRequestFilter |
99 | 102 |
|
100 | 103 | private boolean includePayload = false; |
101 | 104 |
|
| 105 | + private @Nullable Predicate<String> queryParamPredicate; |
| 106 | + |
102 | 107 | private @Nullable Predicate<String> headerPredicate; |
103 | 108 |
|
104 | 109 | private int maxPayloadLength = DEFAULT_MAX_PAYLOAD_LENGTH; |
@@ -182,6 +187,30 @@ protected boolean isIncludePayload() { |
182 | 187 | return this.includePayload; |
183 | 188 | } |
184 | 189 |
|
| 190 | + /** |
| 191 | + * Configure a predicate for selecting which query params should be logged if |
| 192 | + * {@link #setIncludeQueryString(boolean)} is set to {@code true}. |
| 193 | + * <p>By default this is not set in which case all query params are logged |
| 194 | + * |
| 195 | + * <p>If there are multiple values for the same query param, |
| 196 | + * the predicate will be applied once per query param name. |
| 197 | + * As a result, the use of this predicate may result in a different query string |
| 198 | + * than the one returned by {@link HttpServletRequest#getQueryString()}. |
| 199 | + * @param queryParamPredicate the predicate to use |
| 200 | + * @since 7.0 |
| 201 | + */ |
| 202 | + public void setQueryParamPredicate(@Nullable Predicate<String> queryParamPredicate) { |
| 203 | + this.queryParamPredicate = queryParamPredicate; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * The configured {@link #setQueryParamPredicate(Predicate) queryParamPredicate}. |
| 208 | + * @since 7.0 |
| 209 | + */ |
| 210 | + protected @Nullable Predicate<String> getQueryParamPredicate() { |
| 211 | + return this.queryParamPredicate; |
| 212 | + } |
| 213 | + |
185 | 214 | /** |
186 | 215 | * Configure a predicate for selecting which headers should be logged if |
187 | 216 | * {@link #setIncludeHeaders(boolean)} is set to {@code true}. |
@@ -326,6 +355,24 @@ protected String createMessage(HttpServletRequest request, String prefix, String |
326 | 355 | if (isIncludeQueryString()) { |
327 | 356 | String queryString = request.getQueryString(); |
328 | 357 | if (queryString != null) { |
| 358 | + if (getQueryParamPredicate() != null) { |
| 359 | + MultiValueMap<String, String> queryParams = UriComponentsBuilder.fromUriString("?" + queryString) |
| 360 | + .build() |
| 361 | + .getQueryParams(); |
| 362 | + |
| 363 | + MultiValueMap<String, String> updatedQueryParams = new LinkedMultiValueMap<>(queryParams); |
| 364 | + for (String name : queryParams.keySet()) { |
| 365 | + if (!getQueryParamPredicate().test(name)) { |
| 366 | + updatedQueryParams.set(name, "masked"); |
| 367 | + break; |
| 368 | + } |
| 369 | + } |
| 370 | + |
| 371 | + queryString = UriComponentsBuilder.newInstance() |
| 372 | + .queryParams(updatedQueryParams) |
| 373 | + .build() |
| 374 | + .getQuery(); |
| 375 | + } |
329 | 376 | msg.append('?').append(queryString); |
330 | 377 | } |
331 | 378 | } |
|
0 commit comments