Ensure handling of 404 errors for static resources

Closes gh-30930
This commit is contained in:
rstoyanchev
2023-07-27 22:09:18 +03:00
parent 85704c890e
commit 78d0dbb519
6 changed files with 194 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.core.Ordered;
import org.springframework.core.log.LogFormatUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
@@ -98,6 +99,25 @@ public abstract class AbstractHandlerExceptionResolver implements HandlerExcepti
this.mappedHandlerClasses = mappedHandlerClasses;
}
/**
* Add a mapped handler class.
* @since 6.1
*/
public void addMappedHandlerClass(Class<?> mappedHandlerClass) {
this.mappedHandlerClasses = (this.mappedHandlerClasses != null ?
ObjectUtils.addObjectToArray(this.mappedHandlerClasses, mappedHandlerClass) :
new Class<?>[] {mappedHandlerClass});
}
/**
* Return the {@link #setMappedHandlerClasses(Class[]) configured} mapped
* handler classes.
*/
@Nullable
protected Class<?>[] getMappedHandlerClasses() {
return this.mappedHandlerClasses;
}
/**
* Set the log category for warn logging. The name will be passed to the underlying logger
* implementation through Commons Logging, getting interpreted as a log category according

View File

@@ -56,6 +56,7 @@ import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import org.springframework.web.servlet.support.RequestContextUtils;
/**
@@ -372,6 +373,12 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
return !this.exceptionHandlerAdviceCache.isEmpty();
}
@Override
protected boolean shouldApplyTo(HttpServletRequest request, @Nullable Object handler) {
return (handler instanceof ResourceHttpRequestHandler ?
hasGlobalExceptionHandlers() : super.shouldApplyTo(request, handler));
}
/**
* Find an {@code @ExceptionHandler} method and invoke it to handle the raised exception.
*/