Configure ResourceUrlProvider in WebFlux
Prior to this commit, no `ResourceUrlProvider` was configured in WebFlux (no bean was contributed by the WebFlux infrastructure). Also, several `ResourceTransformer` instances that extend the `ResourceTransformerSupport` base class need a `ResourceUrlProvider` to resolve absolute URLs when rewriting resource URLs. At this point, no `ResourceUrlProvider` was configured and they could only resolve relative URLs. This commit contributes a new `ResourceUrlProvider` to the WebFlux configuration; this bean can be reused by the WebFlux infrastructure and application code. This also automatically configure this shared `ResourceUrlProvider` instance on the resource chain where needed. Issue: SPR-17433
This commit is contained in:
@@ -28,6 +28,8 @@ import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.web.reactive.handler.AbstractUrlHandlerMapping;
|
||||
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.reactive.resource.ResourceTransformerSupport;
|
||||
import org.springframework.web.reactive.resource.ResourceUrlProvider;
|
||||
import org.springframework.web.reactive.resource.ResourceWebHandler;
|
||||
import org.springframework.web.server.WebHandler;
|
||||
|
||||
@@ -49,6 +51,7 @@ import org.springframework.web.server.WebHandler;
|
||||
* period for served resources.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Brian Clozel
|
||||
* @since 5.0
|
||||
*/
|
||||
public class ResourceHandlerRegistry {
|
||||
@@ -57,7 +60,10 @@ public class ResourceHandlerRegistry {
|
||||
|
||||
private final List<ResourceHandlerRegistration> registrations = new ArrayList<>();
|
||||
|
||||
private int order = Ordered.LOWEST_PRECEDENCE -1;
|
||||
private int order = Ordered.LOWEST_PRECEDENCE - 1;
|
||||
|
||||
@Nullable
|
||||
private ResourceUrlProvider resourceUrlProvider;
|
||||
|
||||
|
||||
/**
|
||||
@@ -69,6 +75,17 @@ public class ResourceHandlerRegistry {
|
||||
this.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the {@link ResourceUrlProvider} that can be used by
|
||||
* {@link org.springframework.web.reactive.resource.ResourceTransformer} instances.
|
||||
* @param resourceUrlProvider the resource URL provider to use
|
||||
* @since 5.1.2
|
||||
*/
|
||||
public void setResourceUrlProvider(@Nullable ResourceUrlProvider resourceUrlProvider) {
|
||||
this.resourceUrlProvider = resourceUrlProvider;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Add a resource handler for serving static resources based on the specified
|
||||
@@ -121,6 +138,11 @@ public class ResourceHandlerRegistry {
|
||||
for (ResourceHandlerRegistration registration : this.registrations) {
|
||||
for (String pathPattern : registration.getPathPatterns()) {
|
||||
ResourceWebHandler handler = registration.getRequestHandler();
|
||||
handler.getResourceTransformers().forEach(transformer -> {
|
||||
if (transformer instanceof ResourceTransformerSupport) {
|
||||
((ResourceTransformerSupport) transformer).setResourceUrlProvider(this.resourceUrlProvider);
|
||||
}
|
||||
});
|
||||
try {
|
||||
handler.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@@ -55,6 +55,7 @@ import org.springframework.web.reactive.function.server.support.RouterFunctionMa
|
||||
import org.springframework.web.reactive.function.server.support.ServerResponseResultHandler;
|
||||
import org.springframework.web.reactive.handler.AbstractHandlerMapping;
|
||||
import org.springframework.web.reactive.handler.WebFluxResponseStatusExceptionHandler;
|
||||
import org.springframework.web.reactive.resource.ResourceUrlProvider;
|
||||
import org.springframework.web.reactive.result.SimpleHandlerAdapter;
|
||||
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
|
||||
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerAdapter;
|
||||
@@ -74,6 +75,7 @@ import org.springframework.web.server.i18n.LocaleContextResolver;
|
||||
* <p>Import directly or extend and override protected methods to customize.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Brian Clozel
|
||||
* @since 5.0
|
||||
*/
|
||||
public class WebFluxConfigurationSupport implements ApplicationContextAware {
|
||||
@@ -229,6 +231,7 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
|
||||
resourceLoader = new DefaultResourceLoader();
|
||||
}
|
||||
ResourceHandlerRegistry registry = new ResourceHandlerRegistry(resourceLoader);
|
||||
registry.setResourceUrlProvider(resourceUrlProvider());
|
||||
addResourceHandlers(registry);
|
||||
|
||||
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
|
||||
@@ -249,6 +252,11 @@ public class WebFluxConfigurationSupport implements ApplicationContextAware {
|
||||
return handlerMapping;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ResourceUrlProvider resourceUrlProvider() {
|
||||
return new ResourceUrlProvider();
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this method to add resource handlers for serving static resources.
|
||||
* @see ResourceHandlerRegistry
|
||||
|
||||
Reference in New Issue
Block a user