|
|
|
@ -84,6 +84,7 @@ import org.springframework.web.HttpMediaTypeNotAcceptableException;
|
|
|
|
|
import org.springframework.web.accept.ContentNegotiationManager;
|
|
|
|
|
import org.springframework.web.accept.ContentNegotiationStrategy;
|
|
|
|
|
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
|
|
|
|
import org.springframework.web.context.ServletContextAware;
|
|
|
|
|
import org.springframework.web.context.request.NativeWebRequest;
|
|
|
|
|
import org.springframework.web.context.request.RequestAttributes;
|
|
|
|
|
import org.springframework.web.context.request.RequestContextListener;
|
|
|
|
@ -182,7 +183,11 @@ public class WebMvcAutoConfiguration {
|
|
|
|
|
@EnableConfigurationProperties({ WebMvcProperties.class,
|
|
|
|
|
org.springframework.boot.autoconfigure.web.ResourceProperties.class, WebProperties.class })
|
|
|
|
|
@Order(0)
|
|
|
|
|
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
|
|
|
|
|
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer, ServletContextAware {
|
|
|
|
|
|
|
|
|
|
private static final Log logger = LogFactory.getLog(WebMvcConfigurer.class);
|
|
|
|
|
|
|
|
|
|
private final Resources resourceProperties;
|
|
|
|
|
|
|
|
|
|
private final WebMvcProperties mvcProperties;
|
|
|
|
|
|
|
|
|
@ -194,13 +199,19 @@ public class WebMvcAutoConfiguration {
|
|
|
|
|
|
|
|
|
|
private final ObjectProvider<ServletRegistrationBean<?>> servletRegistrations;
|
|
|
|
|
|
|
|
|
|
final ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer;
|
|
|
|
|
private final ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer;
|
|
|
|
|
|
|
|
|
|
private ServletContext servletContext;
|
|
|
|
|
|
|
|
|
|
public WebMvcAutoConfigurationAdapter(WebProperties webProperties, WebMvcProperties mvcProperties,
|
|
|
|
|
ListableBeanFactory beanFactory, ObjectProvider<HttpMessageConverters> messageConvertersProvider,
|
|
|
|
|
public WebMvcAutoConfigurationAdapter(
|
|
|
|
|
org.springframework.boot.autoconfigure.web.ResourceProperties resourceProperties,
|
|
|
|
|
WebProperties webProperties, WebMvcProperties mvcProperties, ListableBeanFactory beanFactory,
|
|
|
|
|
ObjectProvider<HttpMessageConverters> messageConvertersProvider,
|
|
|
|
|
ObjectProvider<ResourceHandlerRegistrationCustomizer> resourceHandlerRegistrationCustomizerProvider,
|
|
|
|
|
ObjectProvider<DispatcherServletPath> dispatcherServletPath,
|
|
|
|
|
ObjectProvider<ServletRegistrationBean<?>> servletRegistrations) {
|
|
|
|
|
this.resourceProperties = resourceProperties.hasBeenCustomized() ? resourceProperties
|
|
|
|
|
: webProperties.getResources();
|
|
|
|
|
this.mvcProperties = mvcProperties;
|
|
|
|
|
this.beanFactory = beanFactory;
|
|
|
|
|
this.messageConvertersProvider = messageConvertersProvider;
|
|
|
|
@ -210,6 +221,11 @@ public class WebMvcAutoConfiguration {
|
|
|
|
|
this.mvcProperties.checkConfiguration();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setServletContext(ServletContext servletContext) {
|
|
|
|
|
this.servletContext = servletContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
|
|
|
|
this.messageConvertersProvider
|
|
|
|
@ -312,6 +328,49 @@ public class WebMvcAutoConfiguration {
|
|
|
|
|
ApplicationConversionService.addBeans(registry, this.beanFactory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
|
|
|
if (!this.resourceProperties.isAddMappings()) {
|
|
|
|
|
logger.debug("Default resource handling disabled");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
|
|
|
|
|
addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
|
|
|
|
|
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
|
|
|
|
|
if (this.servletContext != null) {
|
|
|
|
|
ServletContextResource resource = new ServletContextResource(this.servletContext, SERVLET_LOCATION);
|
|
|
|
|
registration.addResourceLocations(resource);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addResourceHandler(ResourceHandlerRegistry registry, String pattern, String... locations) {
|
|
|
|
|
addResourceHandler(registry, pattern, (registration) -> registration.addResourceLocations(locations));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addResourceHandler(ResourceHandlerRegistry registry, String pattern,
|
|
|
|
|
Consumer<ResourceHandlerRegistration> customizer) {
|
|
|
|
|
if (registry.hasMappingForPattern(pattern)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ResourceHandlerRegistration registration = registry.addResourceHandler(pattern);
|
|
|
|
|
customizer.accept(registration);
|
|
|
|
|
registration.setCachePeriod(getSeconds(this.resourceProperties.getCache().getPeriod()));
|
|
|
|
|
registration.setCacheControl(this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl());
|
|
|
|
|
registration.setUseLastModified(this.resourceProperties.getCache().isUseLastModified());
|
|
|
|
|
customizeResourceHandlerRegistration(registration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Integer getSeconds(Duration cachePeriod) {
|
|
|
|
|
return (cachePeriod != null) ? (int) cachePeriod.getSeconds() : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void customizeResourceHandlerRegistration(ResourceHandlerRegistration registration) {
|
|
|
|
|
if (this.resourceHandlerRegistrationCustomizer != null) {
|
|
|
|
|
this.resourceHandlerRegistrationCustomizer.customize(registration);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
@ConditionalOnMissingBean({ RequestContextListener.class, RequestContextFilter.class })
|
|
|
|
|
@ConditionalOnMissingFilterBean(RequestContextFilter.class)
|
|
|
|
@ -328,8 +387,6 @@ public class WebMvcAutoConfiguration {
|
|
|
|
|
@EnableConfigurationProperties(WebProperties.class)
|
|
|
|
|
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {
|
|
|
|
|
|
|
|
|
|
private static final Log logger = LogFactory.getLog(WebMvcConfigurer.class);
|
|
|
|
|
|
|
|
|
|
private final Resources resourceProperties;
|
|
|
|
|
|
|
|
|
|
private final WebMvcProperties mvcProperties;
|
|
|
|
@ -340,8 +397,6 @@ public class WebMvcAutoConfiguration {
|
|
|
|
|
|
|
|
|
|
private final WebMvcRegistrations mvcRegistrations;
|
|
|
|
|
|
|
|
|
|
private final ResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCustomizer;
|
|
|
|
|
|
|
|
|
|
private ResourceLoader resourceLoader;
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
@ -356,7 +411,6 @@ public class WebMvcAutoConfiguration {
|
|
|
|
|
this.mvcProperties = mvcProperties;
|
|
|
|
|
this.webProperties = webProperties;
|
|
|
|
|
this.mvcRegistrations = mvcRegistrationsProvider.getIfUnique();
|
|
|
|
|
this.resourceHandlerRegistrationCustomizer = resourceHandlerRegistrationCustomizerProvider.getIfAvailable();
|
|
|
|
|
this.beanFactory = beanFactory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -396,50 +450,6 @@ public class WebMvcAutoConfiguration {
|
|
|
|
|
resourceUrlProvider);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
|
|
|
super.addResourceHandlers(registry);
|
|
|
|
|
if (!this.resourceProperties.isAddMappings()) {
|
|
|
|
|
logger.debug("Default resource handling disabled");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ServletContext servletContext = getServletContext();
|
|
|
|
|
addResourceHandler(registry, "/webjars/**", "classpath:/META-INF/resources/webjars/");
|
|
|
|
|
addResourceHandler(registry, this.mvcProperties.getStaticPathPattern(), (registration) -> {
|
|
|
|
|
registration.addResourceLocations(this.resourceProperties.getStaticLocations());
|
|
|
|
|
if (servletContext != null) {
|
|
|
|
|
registration.addResourceLocations(new ServletContextResource(servletContext, SERVLET_LOCATION));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addResourceHandler(ResourceHandlerRegistry registry, String pattern, String... locations) {
|
|
|
|
|
addResourceHandler(registry, pattern, (registration) -> registration.addResourceLocations(locations));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addResourceHandler(ResourceHandlerRegistry registry, String pattern,
|
|
|
|
|
Consumer<ResourceHandlerRegistration> customizer) {
|
|
|
|
|
if (registry.hasMappingForPattern(pattern)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ResourceHandlerRegistration registration = registry.addResourceHandler(pattern);
|
|
|
|
|
customizer.accept(registration);
|
|
|
|
|
registration.setCachePeriod(getSeconds(this.resourceProperties.getCache().getPeriod()));
|
|
|
|
|
registration.setCacheControl(this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl());
|
|
|
|
|
registration.setUseLastModified(this.resourceProperties.getCache().isUseLastModified());
|
|
|
|
|
customizeResourceHandlerRegistration(registration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Integer getSeconds(Duration cachePeriod) {
|
|
|
|
|
return (cachePeriod != null) ? (int) cachePeriod.getSeconds() : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void customizeResourceHandlerRegistration(ResourceHandlerRegistration registration) {
|
|
|
|
|
if (this.resourceHandlerRegistrationCustomizer != null) {
|
|
|
|
|
this.resourceHandlerRegistrationCustomizer.customize(registration);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext,
|
|
|
|
|
FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
|
|
|
|
|