From 5fceb9d5b7e0c6ed32642d2d5bbaed524f13dc33 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 23 Oct 2020 12:03:32 +0200 Subject: [PATCH] Change favicon StaticResourceLocation Prior to this commit, the `StaticResourceLocation` for favicons would point to `"/**/favicon.ico"`. This location does not reflect the current web development landscape, since the png format and size variants are not supported here. Also, the `"**"` pattern can be costly at runtime and is deprecated by the new path pattern support in Spring Framework (see gh-22833). This commit changes the default locations to `"/favicon.*","/*/icon-*"`, supporting common use cases such as `"/favicon.ico"`, `"/favicon.png"` and `"/icons/icon-48x48.png"`. Closes gh-23126 --- .../autoconfigure/security/StaticResourceLocation.java | 2 +- .../security/reactive/StaticResourceRequest.java | 3 +-- .../security/reactive/StaticResourceRequestTests.java | 6 ++++-- .../security/servlet/StaticResourceRequestTests.java | 6 ++++-- .../src/docs/asciidoc/spring-boot-features.adoc | 7 ------- 5 files changed, 10 insertions(+), 14 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/StaticResourceLocation.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/StaticResourceLocation.java index 19c1cd2200..1d6f62ae6b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/StaticResourceLocation.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/StaticResourceLocation.java @@ -50,7 +50,7 @@ public enum StaticResourceLocation { /** * The {@code "favicon.ico"} resource. */ - FAVICON("/**/favicon.ico"); + FAVICON("/favicon.*", "/*/icon-*"); private final String[] patterns; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/reactive/StaticResourceRequest.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/reactive/StaticResourceRequest.java index 9d6b71c122..a1609fa952 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/reactive/StaticResourceRequest.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/reactive/StaticResourceRequest.java @@ -128,8 +128,7 @@ public final class StaticResourceRequest { } private Stream getPatterns() { - return this.locations.stream().flatMap(StaticResourceLocation::getPatterns) - .map((pattern) -> pattern.replace("/**/", "/*/")); + return this.locations.stream().flatMap(StaticResourceLocation::getPatterns); } @Override diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/reactive/StaticResourceRequestTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/reactive/StaticResourceRequestTests.java index 7df939f947..556a1d94e6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/reactive/StaticResourceRequestTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/reactive/StaticResourceRequestTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-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. @@ -54,7 +54,9 @@ class StaticResourceRequestTests { assertMatcher(matcher).matches("/js/file.js"); assertMatcher(matcher).matches("/images/file.css"); assertMatcher(matcher).matches("/webjars/file.css"); - assertMatcher(matcher).matches("/foo/favicon.ico"); + assertMatcher(matcher).matches("/favicon.ico"); + assertMatcher(matcher).matches("/favicon.png"); + assertMatcher(matcher).matches("/icons/icon-48x48.png"); assertMatcher(matcher).doesNotMatch("/bar"); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/StaticResourceRequestTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/StaticResourceRequestTests.java index 60335af7e0..26babb807d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/StaticResourceRequestTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/StaticResourceRequestTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-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. @@ -48,7 +48,9 @@ class StaticResourceRequestTests { assertMatcher(matcher).matches("/js/file.js"); assertMatcher(matcher).matches("/images/file.css"); assertMatcher(matcher).matches("/webjars/file.css"); - assertMatcher(matcher).matches("/foo/favicon.ico"); + assertMatcher(matcher).matches("/favicon.ico"); + assertMatcher(matcher).matches("/favicon.png"); + assertMatcher(matcher).matches("/icons/icon-48x48.png"); assertMatcher(matcher).doesNotMatch("/bar"); } diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc index 29dba35b35..c77e641a30 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc @@ -2639,13 +2639,6 @@ If either is found, it is automatically used as the welcome page of the applicat -[[boot-features-spring-mvc-favicon]] -==== Custom Favicon -As with other static resources, Spring Boot looks for a `favicon.ico` in the configured static content locations. -If such a file is present, it is automatically used as the favicon of the application. - - - [[boot-features-spring-mvc-pathmatch]] ==== Path Matching and Content Negotiation Spring MVC can map incoming HTTP requests to handlers by looking at the request path and matching it to the mappings defined in your application (for example, `@GetMapping` annotations on Controller methods).