diff --git a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java index a5a53391bccf..f94a8d664a5b 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/HttpHeaders.java @@ -24,6 +24,7 @@ import java.nio.charset.StandardCharsets; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; +import java.time.Duration; import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; @@ -586,7 +587,14 @@ public List getAccessControlExposeHeaders() { * Set the (new) value of the {@code Access-Control-Max-Age} response header. */ public void setAccessControlMaxAge(long maxAge) { - set(ACCESS_CONTROL_MAX_AGE, Long.toString(maxAge)); + setAccessControlMaxAge(Duration.ofSeconds(maxAge)); + } + + /** + * Set the (new) value of the {@code Access-Control-Max-Age} response header. + */ + public void setAccessControlMaxAge(Duration maxAge) { + set(ACCESS_CONTROL_MAX_AGE, Long.toString(maxAge.getSeconds())); } /** @@ -932,6 +940,22 @@ public void setDate(long date) { setDate(DATE, date); } + /** + * Set the date and time at which the message was created, as specified + * by the {@code Date} header. + */ + public void setDate(Instant date) { + setInstant(DATE, date); + } + + /** + * Set the date and time at which the message was created, as specified + * by the {@code Date} header. + */ + public void setDate(ZonedDateTime date) { + setZonedDateTime(DATE, date); + } + /** * Return the date and time at which the message was created, as specified * by the {@code Date} header. @@ -985,6 +1009,14 @@ public void setExpires(long expires) { setDate(EXPIRES, expires); } + /** + * Set the date and time at which the message is no longer valid, + * as specified by the {@code Expires} header. + */ + public void setExpires(Instant expires) { + setInstant(EXPIRES, expires); + } + /** * Return the date and time at which the message is no longer valid, * as specified by the {@code Expires} header. diff --git a/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java b/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java index 5478bb6656df..3da34fe703b6 100644 --- a/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java +++ b/spring-web/src/main/java/org/springframework/web/cors/CorsConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.web.cors; +import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -81,8 +82,7 @@ public class CorsConfiguration { private Boolean allowCredentials; @Nullable - private Long maxAge; - + private Duration maxAge; /** * Construct a new {@code CorsConfiguration} instance with no cross-origin @@ -310,6 +310,20 @@ public Boolean getAllowCredentials() { *

By default this is not set. */ public void setMaxAge(@Nullable Long maxAge) { + if (maxAge == null) { + this.maxAge = null; + } + else { + this.maxAge = Duration.ofSeconds(maxAge); + } + } + + /** + * Configure how long, in seconds, the response from a pre-flight request + * can be cached by clients. + *

By default this is not set. + */ + public void setMaxAge(@Nullable Duration maxAge) { this.maxAge = maxAge; } @@ -319,7 +333,7 @@ public void setMaxAge(@Nullable Long maxAge) { */ @Nullable public Long getMaxAge() { - return this.maxAge; + return this.maxAge != null ? this.maxAge.getSeconds() : null; } /** @@ -353,7 +367,7 @@ public CorsConfiguration applyPermitDefaultValues() { this.allowedHeaders = DEFAULT_PERMIT_ALL; } if (this.maxAge == null) { - this.maxAge = 1800L; + this.maxAge = Duration.ofSeconds(1800L); } return this; } diff --git a/spring-web/src/test/java/org/springframework/web/cors/CorsConfigurationTests.java b/spring-web/src/test/java/org/springframework/web/cors/CorsConfigurationTests.java index 2a000c182b17..c42d0ab9e15e 100644 --- a/spring-web/src/test/java/org/springframework/web/cors/CorsConfigurationTests.java +++ b/spring-web/src/test/java/org/springframework/web/cors/CorsConfigurationTests.java @@ -16,6 +16,7 @@ package org.springframework.web.cors; +import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -47,7 +48,7 @@ public void setNullValues() { assertNull(config.getExposedHeaders()); config.setAllowCredentials(null); assertNull(config.getAllowCredentials()); - config.setMaxAge(null); + config.setMaxAge((Duration)null); assertNull(config.getMaxAge()); }