Skip to content

Commit 76664ad

Browse files
wilkinsonapull[bot]
authored andcommitted
Remove default favicon and support for serving from classpath root
Closes spring-projectsgh-17925
1 parent d940e7c commit 76664ad

File tree

5 files changed

+16
-55
lines changed

5 files changed

+16
-55
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -332,27 +332,6 @@ public static RequestContextFilter requestContextFilter() {
332332
return new OrderedRequestContextFilter();
333333
}
334334

335-
@Configuration(proxyBeanMethods = false)
336-
@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
337-
public static class FaviconConfiguration implements WebMvcConfigurer {
338-
339-
private final ResourceProperties resourceProperties;
340-
341-
FaviconConfiguration(ResourceProperties resourceProperties) {
342-
this.resourceProperties = resourceProperties;
343-
}
344-
345-
@Override
346-
public void addResourceHandlers(ResourceHandlerRegistry registry) {
347-
if (!registry.hasMappingForPattern("favicon.ico")) {
348-
registry.addResourceHandler("favicon.ico")
349-
.addResourceLocations(this.resourceProperties.getStaticLocations())
350-
.addResourceLocations("classpath:favicon.ico");
351-
}
352-
}
353-
354-
}
355-
356335
}
357336

358337
/**

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -699,12 +699,6 @@
699699
"name": "spring.mustache.suffix",
700700
"defaultValue": ".mustache"
701701
},
702-
{
703-
"name": "spring.mvc.favicon.enabled",
704-
"type": "java.lang.Boolean",
705-
"description": "Whether to enable resolution of favicon.ico.",
706-
"defaultValue": true
707-
},
708702
{
709703
"name": "spring.mvc.formcontent.putfilter.enabled",
710704
"type": "java.lang.Boolean",

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ void resourceHandlerMappingOverrideAll() {
196196
@Test
197197
void resourceHandlerMappingDisabled() {
198198
this.contextRunner.withPropertyValues("spring.resources.add-mappings:false")
199-
.run((context) -> assertThat(getResourceMappingLocations(context)).hasSize(1));
199+
.run((context) -> assertThat(getResourceMappingLocations(context)).hasSize(0));
200200
}
201201

202202
@Test
@@ -378,21 +378,6 @@ void customContentNegotiatingViewResolver() {
378378
.containsOnly("myViewResolver"));
379379
}
380380

381-
@Test
382-
void faviconMapping() {
383-
this.contextRunner.run((context) -> {
384-
List<Resource> favIconResources = getResourceMappingLocations(context).get("/favicon.ico");
385-
assertThat(favIconResources.stream().map(ClassPathResource.class::cast).map(ClassPathResource::getPath))
386-
.containsExactly("META-INF/resources/", "resources/", "static/", "public/", "favicon.ico");
387-
});
388-
}
389-
390-
@Test
391-
void faviconMappingDisabled() {
392-
this.contextRunner.withPropertyValues("spring.mvc.favicon.enabled:false")
393-
.run((context) -> assertThat(getResourceMappingLocations(context).get("/favicon.ico")).isNull());
394-
}
395-
396381
@Test
397382
void defaultAsyncRequestTimeout() {
398383
this.contextRunner.run((context) -> assertThat(ReflectionTestUtils
@@ -660,14 +645,12 @@ void cachePeriod() {
660645

661646
private void assertCachePeriod(AssertableWebApplicationContext context) {
662647
Map<String, Object> handlerMap = getHandlerMap(context.getBean("resourceHandlerMapping", HandlerMapping.class));
663-
assertThat(handlerMap).hasSize(3);
648+
assertThat(handlerMap).hasSize(2);
664649
for (Entry<String, Object> entry : handlerMap.entrySet()) {
665-
if (!entry.getKey().equals("/favicon.ico")) {
666-
Object handler = entry.getValue();
667-
if (handler instanceof ResourceHttpRequestHandler) {
668-
assertThat(((ResourceHttpRequestHandler) handler).getCacheSeconds()).isEqualTo(5);
669-
assertThat(((ResourceHttpRequestHandler) handler).getCacheControl()).isNull();
670-
}
650+
Object handler = entry.getValue();
651+
if (handler instanceof ResourceHttpRequestHandler) {
652+
assertThat(((ResourceHttpRequestHandler) handler).getCacheSeconds()).isEqualTo(5);
653+
assertThat(((ResourceHttpRequestHandler) handler).getCacheControl()).isNull();
671654
}
672655
}
673656
}
@@ -784,7 +767,7 @@ void customPrinterAndParserShouldBeRegisteredAsConverters() {
784767

785768
private void assertCacheControl(AssertableWebApplicationContext context) {
786769
Map<String, Object> handlerMap = getHandlerMap(context.getBean("resourceHandlerMapping", HandlerMapping.class));
787-
assertThat(handlerMap).hasSize(3);
770+
assertThat(handlerMap).hasSize(2);
788771
for (Object handler : handlerMap.keySet()) {
789772
if (handler instanceof ResourceHttpRequestHandler) {
790773
assertThat(((ResourceHttpRequestHandler) handler).getCacheSeconds()).isEqualTo(-1);
@@ -795,7 +778,12 @@ private void assertCacheControl(AssertableWebApplicationContext context) {
795778
}
796779

797780
protected Map<String, List<Resource>> getResourceMappingLocations(ApplicationContext context) {
798-
return getMappingLocations(context.getBean("resourceHandlerMapping", HandlerMapping.class));
781+
Object bean = context.getBean("resourceHandlerMapping");
782+
if (bean instanceof HandlerMapping) {
783+
return getMappingLocations(context.getBean("resourceHandlerMapping", HandlerMapping.class));
784+
}
785+
assertThat(bean.toString()).isEqualTo("null");
786+
return Collections.emptyMap();
799787
}
800788

801789
protected List<ResourceResolver> getResourceResolvers(ApplicationContext context, String mapping) {

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,9 +2508,9 @@ welcome page of the application.
25082508

25092509
[[boot-features-spring-mvc-favicon]]
25102510
==== Custom Favicon
2511-
Spring Boot looks for a `favicon.ico` in the configured static content locations and the
2512-
root of the classpath (in that order). If such a file is present, it is automatically
2513-
used as the favicon of the application.
2511+
As with other static resources, Spring Boot looks for a `favicon.ico` in the configured
2512+
static content locations. If such a file is present, it is automatically used as the
2513+
favicon of the application.
25142514

25152515

25162516
[[boot-features-spring-mvc-pathmatch]]
Binary file not shown.

0 commit comments

Comments
 (0)