mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
parent
55f7b3a535
commit
e3c3bb0076
@ -23,7 +23,6 @@ import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.bind.convert.DefaultDurationUnit;
|
||||
import org.springframework.http.CacheControl;
|
||||
|
||||
/**
|
||||
* Properties used to configure resource handling.
|
||||
@ -275,7 +274,7 @@ public class ResourceProperties {
|
||||
/**
|
||||
* Cache period for the resources served by the resource handler. If a duration
|
||||
* suffix is not specified, seconds will be used. Can be overridden by the
|
||||
* 'spring.resources.cache.control' properties.
|
||||
* 'spring.resources.cache.cachecontrol' properties.
|
||||
*/
|
||||
@DefaultDurationUnit(ChronoUnit.SECONDS)
|
||||
private Duration period;
|
||||
@ -284,7 +283,7 @@ public class ResourceProperties {
|
||||
* Cache control HTTP headers, only allows valid directive combinations. Overrides
|
||||
* the 'spring.resources.cache.period' property.
|
||||
*/
|
||||
private final Control control = new Control();
|
||||
private final CacheControl cacheControl = new CacheControl();
|
||||
|
||||
public Duration getPeriod() {
|
||||
return this.period;
|
||||
@ -294,14 +293,14 @@ public class ResourceProperties {
|
||||
this.period = period;
|
||||
}
|
||||
|
||||
public Control getControl() {
|
||||
return this.control;
|
||||
public CacheControl getCacheControl() {
|
||||
return this.cacheControl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache Control HTTP header configuration.
|
||||
*/
|
||||
public static class Control {
|
||||
public static class CacheControl {
|
||||
|
||||
/**
|
||||
* Maximum time the response should be cached, in seconds if no duration
|
||||
@ -459,15 +458,15 @@ public class ResourceProperties {
|
||||
this.sMaxAge = sMaxAge;
|
||||
}
|
||||
|
||||
public CacheControl toHttpCacheControl() {
|
||||
CacheControl cacheControl = createCacheControl();
|
||||
public org.springframework.http.CacheControl toHttpCacheControl() {
|
||||
org.springframework.http.CacheControl cacheControl = createCacheControl();
|
||||
callIfTrue(this.mustRevalidate, cacheControl,
|
||||
CacheControl::mustRevalidate);
|
||||
callIfTrue(this.noTransform, cacheControl, CacheControl::noTransform);
|
||||
callIfTrue(this.cachePublic, cacheControl, CacheControl::cachePublic);
|
||||
callIfTrue(this.cachePrivate, cacheControl, CacheControl::cachePrivate);
|
||||
org.springframework.http.CacheControl::mustRevalidate);
|
||||
callIfTrue(this.noTransform, cacheControl, org.springframework.http.CacheControl::noTransform);
|
||||
callIfTrue(this.cachePublic, cacheControl, org.springframework.http.CacheControl::cachePublic);
|
||||
callIfTrue(this.cachePrivate, cacheControl, org.springframework.http.CacheControl::cachePrivate);
|
||||
callIfTrue(this.proxyRevalidate, cacheControl,
|
||||
CacheControl::proxyRevalidate);
|
||||
org.springframework.http.CacheControl::proxyRevalidate);
|
||||
if (this.staleWhileRevalidate != null) {
|
||||
cacheControl.staleWhileRevalidate(
|
||||
this.staleWhileRevalidate.getSeconds(), TimeUnit.SECONDS);
|
||||
@ -482,18 +481,18 @@ public class ResourceProperties {
|
||||
return cacheControl;
|
||||
}
|
||||
|
||||
private CacheControl createCacheControl() {
|
||||
private org.springframework.http.CacheControl createCacheControl() {
|
||||
if (Boolean.TRUE.equals(this.noStore)) {
|
||||
return CacheControl.noStore();
|
||||
return org.springframework.http.CacheControl.noStore();
|
||||
}
|
||||
if (Boolean.TRUE.equals(this.noCache)) {
|
||||
return CacheControl.noCache();
|
||||
return org.springframework.http.CacheControl.noCache();
|
||||
}
|
||||
if (this.maxAge != null) {
|
||||
return CacheControl.maxAge(this.maxAge.getSeconds(),
|
||||
return org.springframework.http.CacheControl.maxAge(this.maxAge.getSeconds(),
|
||||
TimeUnit.SECONDS);
|
||||
}
|
||||
return CacheControl.empty();
|
||||
return org.springframework.http.CacheControl.empty();
|
||||
}
|
||||
|
||||
private <T> void callIfTrue(Boolean property, T instance, Consumer<T> call) {
|
||||
|
@ -307,7 +307,7 @@ public class WebMvcAutoConfiguration {
|
||||
return;
|
||||
}
|
||||
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
|
||||
CacheControl cacheControl = this.resourceProperties.getCache().getControl()
|
||||
CacheControl cacheControl = this.resourceProperties.getCache().getCacheControl()
|
||||
.toHttpCacheControl();
|
||||
if (!registry.hasMappingForPattern("/webjars/**")) {
|
||||
customizeResourceHandlerRegistration(
|
||||
|
@ -22,7 +22,6 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ResourceProperties.Cache;
|
||||
import org.springframework.boot.testsupport.assertj.Matched;
|
||||
import org.springframework.http.CacheControl;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.hamcrest.CoreMatchers.endsWith;
|
||||
@ -74,14 +73,14 @@ public class ResourcePropertiesTests {
|
||||
|
||||
@Test
|
||||
public void emptyCacheControl() {
|
||||
CacheControl cacheControl = this.properties.getCache().getControl()
|
||||
org.springframework.http.CacheControl cacheControl = this.properties.getCache().getCacheControl()
|
||||
.toHttpCacheControl();
|
||||
assertThat(cacheControl.getHeaderValue()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cacheControlAllPropertiesSet() {
|
||||
Cache.Control properties = this.properties.getCache().getControl();
|
||||
Cache.CacheControl properties = this.properties.getCache().getCacheControl();
|
||||
properties.setMaxAge(Duration.ofSeconds(4));
|
||||
properties.setCachePrivate(true);
|
||||
properties.setCachePublic(true);
|
||||
@ -91,7 +90,7 @@ public class ResourcePropertiesTests {
|
||||
properties.setSMaxAge(Duration.ofSeconds(5));
|
||||
properties.setStaleIfError(Duration.ofSeconds(6));
|
||||
properties.setStaleWhileRevalidate(Duration.ofSeconds(7));
|
||||
CacheControl cacheControl = properties.toHttpCacheControl();
|
||||
org.springframework.http.CacheControl cacheControl = properties.toHttpCacheControl();
|
||||
assertThat(cacheControl.getHeaderValue()).isEqualTo(
|
||||
"max-age=4, must-revalidate, no-transform, public, private, proxy-revalidate,"
|
||||
+ " s-maxage=5, stale-if-error=6, stale-while-revalidate=7");
|
||||
@ -99,10 +98,10 @@ public class ResourcePropertiesTests {
|
||||
|
||||
@Test
|
||||
public void invalidCacheControlCombination() {
|
||||
Cache.Control properties = this.properties.getCache().getControl();
|
||||
Cache.CacheControl properties = this.properties.getCache().getCacheControl();
|
||||
properties.setMaxAge(Duration.ofSeconds(4));
|
||||
properties.setNoStore(true);
|
||||
CacheControl cacheControl = properties.toHttpCacheControl();
|
||||
org.springframework.http.CacheControl cacheControl = properties.toHttpCacheControl();
|
||||
assertThat(cacheControl.getHeaderValue()).isEqualTo("no-store");
|
||||
}
|
||||
|
||||
|
@ -733,8 +733,8 @@ public class WebMvcAutoConfigurationTests {
|
||||
@Test
|
||||
public void cacheControl() throws Exception {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.resources.cache.control.max-age:5",
|
||||
"spring.resources.cache.control.proxy-revalidate:true")
|
||||
.withPropertyValues("spring.resources.cache.cachecontrol.max-age:5",
|
||||
"spring.resources.cache.cachecontrol.proxy-revalidate:true")
|
||||
.run((context) -> assertCacheControl(context));
|
||||
}
|
||||
|
||||
|
@ -399,17 +399,17 @@ content into your application. Rather, pick only the properties that you need.
|
||||
|
||||
# SPRING RESOURCES HANDLING ({sc-spring-boot-autoconfigure}/web/ResourceProperties.{sc-ext}[ResourceProperties])
|
||||
spring.resources.add-mappings=true # Whether to enable default resource handling.
|
||||
spring.resources.cache.control.max-age= # Maximum time the response should be cached, in seconds if no duration suffix is not specified.
|
||||
spring.resources.cache.control.no-cache= # Indicate that the cached response can be reused only if re-validated with the server.
|
||||
spring.resources.cache.control.no-store= # Indicate to not cache the response in any case.
|
||||
spring.resources.cache.control.must-revalidate= # Indicate that once it has become stale, a cache must not use the response without re-validating it with the server.
|
||||
spring.resources.cache.control.no-transform= # Indicate intermediaries (caches and others) that they should not transform the response content.
|
||||
spring.resources.cache.control.cache-public= # Indicate that any cache may store the response.
|
||||
spring.resources.cache.control.cache-private= # Indicate that the response message is intended for a single user and must not be stored by a shared cache.
|
||||
spring.resources.cache.control.proxy-revalidate= # Same meaning as the "must-revalidate" directive, except that it does not apply to private caches.
|
||||
spring.resources.cache.control.stale-while-revalidate= # Maximum time the response can be served after it becomes stale, in seconds if no duration suffix is not specified.
|
||||
spring.resources.cache.control.stale-if-error= # Maximum time the response may be used when errors are encountered, in seconds if no duration suffix is not specified.
|
||||
spring.resources.cache.control.s-max-age= # Maximum time the response should be cached by shared caches, in seconds if no duration suffix is not specified.
|
||||
spring.resources.cache.cachecontrol.max-age= # Maximum time the response should be cached, in seconds if no duration suffix is not specified.
|
||||
spring.resources.cache.cachecontrol.no-cache= # Indicate that the cached response can be reused only if re-validated with the server.
|
||||
spring.resources.cache.cachecontrol.no-store= # Indicate to not cache the response in any case.
|
||||
spring.resources.cache.cachecontrol.must-revalidate= # Indicate that once it has become stale, a cache must not use the response without re-validating it with the server.
|
||||
spring.resources.cache.cachecontrol.no-transform= # Indicate intermediaries (caches and others) that they should not transform the response content.
|
||||
spring.resources.cache.cachecontrol.cache-public= # Indicate that any cache may store the response.
|
||||
spring.resources.cache.cachecontrol.cache-private= # Indicate that the response message is intended for a single user and must not be stored by a shared cache.
|
||||
spring.resources.cache.cachecontrol.proxy-revalidate= # Same meaning as the "must-revalidate" directive, except that it does not apply to private caches.
|
||||
spring.resources.cache.cachecontrol.stale-while-revalidate= # Maximum time the response can be served after it becomes stale, in seconds if no duration suffix is not specified.
|
||||
spring.resources.cache.cachecontrol.stale-if-error= # Maximum time the response may be used when errors are encountered, in seconds if no duration suffix is not specified.
|
||||
spring.resources.cache.cachecontrol.s-max-age= # Maximum time the response should be cached by shared caches, in seconds if no duration suffix is not specified.
|
||||
spring.resources.cache.period= # Cache period for the resources served by the resource handler. If a duration suffix is not specified, seconds will be used.
|
||||
spring.resources.chain.cache=true # Whether to enable caching in the Resource chain.
|
||||
spring.resources.chain.enabled= # Whether to enable the Spring Resource Handling chain. By default, disabled unless at least one strategy has been enabled.
|
||||
|
Loading…
Reference in New Issue
Block a user