Relax SPR-13867 changes for ResourceHttpRequestHandler
Prior to this change, SPR-13867 made sure that any class extending WebContentGenerator would not overwrite existing HTTP "Cache-Control" response headers - set by a filter, a Controller handler, etc. This caused issues with resource handling, since specifying a cache configuration there would not overwrite default headers set by filters, for example by Spring Security. This commit restricts the previous changes to the RequestMappingHandlerAdapter, in order to avoid overwriting header set by a filter or a Controller handler in those cases. Issue: SPR-14005
This commit is contained in:
@@ -747,12 +747,14 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter
|
|||||||
mav = invokeHandlerMethod(request, response, handlerMethod);
|
mav = invokeHandlerMethod(request, response, handlerMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!response.containsHeader(HEADER_CACHE_CONTROL)) {
|
||||||
if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
|
if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
|
||||||
applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
|
applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
prepareResponse(response);
|
prepareResponse(response);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return mav;
|
return mav;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
|
|||||||
|
|
||||||
private static final String HEADER_EXPIRES = "Expires";
|
private static final String HEADER_EXPIRES = "Expires";
|
||||||
|
|
||||||
private static final String HEADER_CACHE_CONTROL = "Cache-Control";
|
protected static final String HEADER_CACHE_CONTROL = "Cache-Control";
|
||||||
|
|
||||||
|
|
||||||
/** Set of supported HTTP methods */
|
/** Set of supported HTTP methods */
|
||||||
@@ -372,7 +372,6 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
|
|||||||
* @since 4.2
|
* @since 4.2
|
||||||
*/
|
*/
|
||||||
protected final void applyCacheControl(HttpServletResponse response, CacheControl cacheControl) {
|
protected final void applyCacheControl(HttpServletResponse response, CacheControl cacheControl) {
|
||||||
if (!response.containsHeader(HEADER_CACHE_CONTROL)) {
|
|
||||||
String ccValue = cacheControl.getHeaderValue();
|
String ccValue = cacheControl.getHeaderValue();
|
||||||
if (ccValue != null) {
|
if (ccValue != null) {
|
||||||
// Set computed HTTP 1.1 Cache-Control header
|
// Set computed HTTP 1.1 Cache-Control header
|
||||||
@@ -384,7 +383,6 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply the given cache seconds and generate corresponding HTTP headers,
|
* Apply the given cache seconds and generate corresponding HTTP headers,
|
||||||
@@ -397,7 +395,6 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds) {
|
protected final void applyCacheSeconds(HttpServletResponse response, int cacheSeconds) {
|
||||||
if (!response.containsHeader(HEADER_CACHE_CONTROL)) {
|
|
||||||
if (this.useExpiresHeader || !this.useCacheControlHeader) {
|
if (this.useExpiresHeader || !this.useCacheControlHeader) {
|
||||||
// Deprecated HTTP 1.0 cache behavior, as in previous Spring versions
|
// Deprecated HTTP 1.0 cache behavior, as in previous Spring versions
|
||||||
if (cacheSeconds > 0) {
|
if (cacheSeconds > 0) {
|
||||||
@@ -424,7 +421,6 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
|
|||||||
applyCacheControl(response, cControl);
|
applyCacheControl(response, cControl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -549,6 +549,17 @@ public class ResourceHttpRequestHandlerTests {
|
|||||||
assertEquals(0, this.response.getContentLength());
|
assertEquals(0, this.response.getContentLength());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SPR-14005
|
||||||
|
@Test
|
||||||
|
public void doOverwriteExistingCacheControlHeaders() throws Exception {
|
||||||
|
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
|
||||||
|
this.response.setHeader("Cache-Control", "no-store");
|
||||||
|
|
||||||
|
this.handler.handleRequest(this.request, this.response);
|
||||||
|
|
||||||
|
assertEquals("max-age=3600", this.response.getHeader("Cache-Control"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private long dateHeaderAsLong(String responseHeaderName) throws Exception {
|
private long dateHeaderAsLong(String responseHeaderName) throws Exception {
|
||||||
return dateFormat.parse(this.response.getHeader(responseHeaderName)).getTime();
|
return dateFormat.parse(this.response.getHeader(responseHeaderName)).getTime();
|
||||||
|
|||||||
Reference in New Issue
Block a user