-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suppress logging from HandlerMapping when used for Spring Security's mvcRequestMatcher #30349
Comments
Briefly looking at it, it appears that piece of code uses a To quote
I think that would fit the bill, @rstoyanchev any insight? (if that's the case, this issue would be |
Indeed this is due to the use of |
Maybe both? Even if resolving will be done only once by Spring Security, the log will look like:
The first At the moment the code for logging this looks like this: if (logger.isTraceEnabled()) {
logger.trace("Mapped to " + handler);
}
else if (logger.isDebugEnabled() && !DispatcherType.ASYNC.equals(request.getDispatcherType())) {
logger.debug("Mapped to " + executionChain.getHandler());
} Probably, we could use For example there is a similar pattern in the if (mappingsLogger.isDebugEnabled()) {
mappingsLogger.debug(formatMappingName() + " " + getHandlerMap());
}
else if ((logger.isDebugEnabled() && !getHandlerMap().isEmpty()) || logger.isTraceEnabled()) {
logger.debug("Detected " + getHandlerMap().size() + " mappings in " + formatMappingName());
} But this will make even second |
Logging is now suppressed with a request attribute as the trigger. |
Wow, very creative solution! Thanks! 👍 |
Affects: spring-web 6.0.7, probably any other version too, as the root cause is
mvcMatchers
andHandlerMappingIntrospector
.When autorization configuration in
HttpSecurity
contains many request matchers andorg.springframework.web
logger is set todebug
, then logs are spammed with message about selected handler mapper on each request (i.e. "Mapped to package.Controller#handler()"). The morerequestMatchers
configured (or even individual URLs in the same request matcher), the more log lines with the same mapping message appears.Example (spring-boot 3.0.5, spring-security 6.0.2):
Then
curl -v "http://localhost:8080/test"
will result with these logs:While the last
Mapped to
log message (that is afterDispatcherServlet
log) is useful, other 10 are confusing and "spamming". And also totally useless. Why would I want to know what controller is matched to the current request every time whenMvcRequestMatcher
checks current request's url against each configured URL? If the log would display something like "/readyz is not matched against /test" (even for each matcher) it would be useful, but resolving current request to a controller's method is just a side effect of the request matching algorithm and the result of such resolving should not be shown on the debug level, IMHO. Especially for each request matcher in the scope of the same request.The root cause is probably the usage of
MvcRequestMatcher
by default and its delegation toHandlerMappingIntrospector
that callshandlerMapping.getHandler(request)
.AbstractHandlerMapping.getHandler(HttpServletRequest)
then logs that message.Changing
requestMatchers(String)
torequestMatchers(AntPathRequestMatcher)
solves the problem.E.g. changing
with
makes 2 less
Mapped to
messages to be logged (9 instead of 11).To be honest, I don't know how to solve this. Probably
AbstractHandlerMapping.getHandler(HttpServletRequest)
should not log ondebug
level? And the caller of this method (e.g.DispatcherServlet
) should log the resolved mapping instead, if it's appropriate. This way if the resolving is done not for the purpose of request dispatching but for e.g. comparison with security rule, the result of resolving will not be logged.HandlerMappingIntrospector
could also log the result, if needed, but probably on atrace
level.This problem probably exists for a long time, but previously most popular request matcher was
antMatcher
. Now themvcMatcher
is the default if spring-webmvc is on classpath (like 99% of the time) so the problem just came to the surface.The text was updated successfully, but these errors were encountered: