From 86b8a70ce6ae0c8864fe06d7dec0e7c809bc2440 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 20 Nov 2023 11:27:04 +0100 Subject: [PATCH] Ensure PathResourceResolvers log warnings for non-existent resources Prior to this commit, the getResource() methods in PathResourceResolver implementations allowed an exception thrown from Resource#getURL() to propagate instead of logging a warning about the missing resource as intended. This commit modifies the getResource() methods in PathResourceResolver implementations so that the log messages include the output of the toString() implementations of the underlying resources instead of their getURL() implementations, which may throw an exception. Furthermore, logging the toString() output of resources aligns with the existing output for "allowed locations" in the same log message. Note that the toString() implementations could potentially also throw exceptions, but that is considered less likely. See gh-31623 Closes gh-31624 (cherry picked from commit 7d2ea7e7e15afb8d3137a1d2667101e4818937ae) --- .../web/reactive/resource/PathResourceResolver.java | 6 +++--- .../web/servlet/resource/PathResourceResolver.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java index dbc86614364..4b61143b803 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2023 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. @@ -123,8 +123,8 @@ public class PathResourceResolver extends AbstractResourceResolver { Resource[] allowed = getAllowedLocations(); logger.warn(LogFormatUtils.formatValue( "Resource path \"" + resourcePath + "\" was successfully resolved " + - "but resource \"" + resource.getURL() + "\" is neither under the " + - "current location \"" + location.getURL() + "\" nor under any of the " + + "but resource \"" + resource + "\" is neither under the " + + "current location \"" + location + "\" nor under any of the " + "allowed locations " + (allowed != null ? Arrays.asList(allowed) : "[]"), -1, true)); } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java index 6704b98d503..3902a6ae7a6 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java @@ -195,8 +195,8 @@ public class PathResourceResolver extends AbstractResourceResolver { Resource[] allowed = getAllowedLocations(); logger.warn(LogFormatUtils.formatValue( "Resource path \"" + resourcePath + "\" was successfully resolved " + - "but resource \"" + resource.getURL() + "\" is neither under " + - "the current location \"" + location.getURL() + "\" nor under any of " + + "but resource \"" + resource + "\" is neither under " + + "the current location \"" + location + "\" nor under any of " + "the allowed locations " + (allowed != null ? Arrays.asList(allowed) : "[]"), -1, true)); } }