Browse Source

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
pull/23886/head
Brian Clozel 5 years ago
parent
commit
5fceb9d5b7
  1. 2
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/StaticResourceLocation.java
  2. 3
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/reactive/StaticResourceRequest.java
  3. 6
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/reactive/StaticResourceRequestTests.java
  4. 6
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/StaticResourceRequestTests.java
  5. 7
      spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc

2
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/StaticResourceLocation.java

@ -50,7 +50,7 @@ public enum StaticResourceLocation { @@ -50,7 +50,7 @@ public enum StaticResourceLocation {
/**
* The {@code "favicon.ico"} resource.
*/
FAVICON("/**/favicon.ico");
FAVICON("/favicon.*", "/*/icon-*");
private final String[] patterns;

3
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 { @@ -128,8 +128,7 @@ public final class StaticResourceRequest {
}
private Stream<String> getPatterns() {
return this.locations.stream().flatMap(StaticResourceLocation::getPatterns)
.map((pattern) -> pattern.replace("/**/", "/*/"));
return this.locations.stream().flatMap(StaticResourceLocation::getPatterns);
}
@Override

6
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/reactive/StaticResourceRequestTests.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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");
}

6
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/StaticResourceRequestTests.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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");
}

7
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 @@ -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).

Loading…
Cancel
Save