-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Add support for detecting only local beans for DispatcherServlet #35738
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
base: main
Are you sure you want to change the base?
Add support for detecting only local beans for DispatcherServlet #35738
Conversation
Signed-off-by: Filip Hrisafov <filip.hrisafov@gmail.com>
|
Thanks for sharing that @bclozel, yes I did notice the improvements in the We had something like: public class ActuatorRequestMatcher extends ApplicationContextRequestMatcher<WebApplicationContext> {
private static final RequestMatcher EMPTY_MATCHER = (request) -> false;
private volatile RequestMatcher delegate;
public ActuatorRequestMatcher() {
super(WebApplicationContext.class);
}
@Override
protected final void initialized(Supplier<WebApplicationContext> context) {
this.delegate = createDelegate(context.get());
}
@Override
protected final boolean matches(HttpServletRequest request,
Supplier<WebApplicationContext> context) {
return this.delegate.matches(request);
}
private RequestMatcher createDelegate(WebApplicationContext context) {
try {
String pathPrefix = getPathPrefix(context);
RequestMatcherFactory requestMatcherFactory = new RequestMatcherFactory(pathPrefix);
return createDelegate(context, requestMatcherFactory);
} catch (NoSuchBeanDefinitionException ex) {
return EMPTY_MATCHER;
}
}
private String getPathPrefix(WebApplicationContext context) {
try {
return context.getBean(DispatcherServletPath.class).getPrefix();
} catch (NoSuchBeanDefinitionException ex) {
return "";
}
}
protected RequestMatcher createDelegate(WebApplicationContext context, RequestMatcherFactory requestMatcherFactory) {
WebEndpointProperties properties = context.getBean(WebEndpointProperties.class);
if (StringUtils.isNotEmpty(properties.getBasePath())) {
return new OrRequestMatcher(
requestMatcherFactory.antPath(properties.getBasePath() + "/**"),
requestMatcherFactory.antPath("/**" + properties.getBasePath() + "/**")
);
}
return EMPTY_MATCHER;
}
private static class RequestMatcherFactory {
private final String servletPath;
RequestMatcherFactory(String servletPath) {
this.servletPath = servletPath;
}
public RequestMatcher antPath(String part) {
String pattern = (this.servletPath.equals("/") ? "" : this.servletPath);
return new AntPathRequestMatcher(pattern + part);
}
}
}Which was inspired by the Spring Boot http
.securityMatcher(new ActuatorRequestMatcher())
.authorizeHttpRequests(configurer -> configurer
.requestMatchers(EndpointRequest.to(InfoEndpoint.class, HealthEndpoint.class)).permitAll()
.requestMatchers(EndpointRequest.toAnyEndpoint()).hasAuthority(SecurityConstants.ACCESS_ACTUATORS)
.anyRequest().denyAll()
)
.httpBasic(Customizer.withDefaults());We did this long time ago, and back then I was not that well versed in the lower levels of the |
|
Ah, thanks for the additional information. |
|
What about a custom |
|
Thanks @rstoyanchev, that might work as well, I didn't think about that. My other approach, was something like: protected RequestMatcher createDelegate(WebApplicationContext context, PathPatternRequestMatcher.Builder pathMatcherBuilder) {
WebEndpointProperties properties = context.getBean(WebEndpointProperties.class);
if (StringUtils.isNotEmpty(properties.getBasePath())) {
Collection<String> urlMappings = new ArrayList<>();
for (ServletRegistrationBean<?> registrationBean : context.getBeansOfType(ServletRegistrationBean.class).values()) {
if (registrationBean instanceof DispatcherServletRegistrationBean) {
continue;
}
if (registrationBean.getServlet() instanceof DispatcherServlet) {
urlMappings.addAll(registrationBean.getUrlMappings());
}
}
List<RequestMatcher> matchers = new ArrayList<>(urlMappings.size() + 1);
matchers.add(pathMatcherBuilder.matcher(properties.getBasePath() + "/**"));
for (String urlMapping : urlMappings) {
String url = urlMapping.endsWith("*") ? urlMapping.substring(0, urlMapping.length() - 1) : urlMapping;
matchers.add(pathMatcherBuilder.matcher(url + properties.getBasePath() + "/**"));
}
return new OrRequestMatcher(matchers);
}
return EMPTY_MATCHER;
}In any case, for us the preferred way is now to go with making sure to only get handler mappings from the child context. I think that something like this would be beneficial in general for the |
The goal of this PR is to be able to configure the
DispatcherServletto only look for beans in the local application context, i.e. do not look in all ancestors.The PR is currently only doing this for the
HandlerMapping(s). However, if this is something that is acceptable for the Spring Team then I can adjust the rest of the beans in the dispatcher servlet:handlerAdaptershandlerExceptionResolversviewResolversThe reason for this is that we are using different
DispatcherServlet(s) to expose different REST APIs and with Spring Boot Actuator the actuator endpoints were being exposed in our servlets as well. We have remedied that using Spring Security and theAntPathMatcherto block access to**/actuator/*. However, theAntPathMatcherwill no longer be there in Spring 7 and I wanted to find a better solution.I did manage to find
WebMvcEndpointChildContextConfigurationfrom Spring Boot, that is achieving exactly what we are looking for. However, I wanted to see if we can avoid the need of all thoseCompositeHandlerMapping,CompositeHandlerAdapter, etc. and let theDispatcherServletitself handle this.If this gets accepted then the different composites in Spring Boot can be removed, and the Actuator management
DispatcherServletwould only need to changeto something like
I'm also open to adjusting the naming of the methods as well. It does not have to be exactly like I've done in this PR.
Thanks for taking the time and considering this PR