question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Request-scoped beans do not work in the management context when it's using a separate port

See original GitHub issue

Hi,

I have a question. We are using OAuth2 for securing our API’s including the management endpoints. However, we are having an issue when authenticating the requests for the management URL’s. Spring Security’s OAuth2AuthenticationManager requires at some point a request scoped OAuth2ClientContext. The problem is that this request scoped bean can not be created. It logs the following warning:

Could not fetch user details: class org.springframework.beans.factory.BeanCreationException, Error creating bean with name 'scopedTarget.oauth2ClientContext': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

After googling this issue, it was suggested to make sure that the request is passing the RequestContextFilter. I was convinced that this was already done by Spring Boot’s auto configuration. Then I noticed that this filter is not used for actuator URL’s. The opposite is also true for non-actuator URL’s. Is there a reason why this filter is not automatically applied for actuator endpoints? What do I have to do to make sure that actuator endpoints also make use of that specific filter?

Additional info: Actuator endpoints are configured to be served on a different port and context root compared to the standard application endpoints.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
wilkinsonacommented, Mar 20, 2018

I see you’ve edited your original description to add the following:

Actuator endpoints are configured to be served on a different port and context root compared to the standard application endpoints.

As you’ve described, that’s a key piece of information as the problem doesn’t occur without the different port.

The above described behaviours was already present in version 1.5.x and is still the same for 2.x. So perhaps this is still a bug?

I’m not sure if we’ll consider it to be a bug, but it’s certainly something that I don’t think we’ve considered before. While we decide what to do, you can configure the separate management context with a RequestContextFilter by creating a class annotated with @ManagementContextConfiguration in a package that isn’t covered by component scanning:

package sample;

import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.filter.RequestContextFilter;

@ManagementContextConfiguration
public class ManagementFilterConfiguration {

	@Bean
	public RequestContextFilter requestContextFilter() {
		return new RequestContextFilter();
	}

}

And then listing it in a META-INF/spring.factories file:

org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=sample.ManagementFilterConfiguration
0reactions
wilkinsonacommented, Mar 28, 2018

Let’s auto-configure the request context filter in the separate management context.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Autowiring request scoped beans into application scoped beans
This means that ApplicationScopedBean will only be created once (at this point there will be no request in flight and so the autowiring...
Read more >
Spring Accessing Request Scope Beans outside of web request
Spring request scope beans are a very convenient way to share request data between multiple places without passing parameters all the way.
Read more >
Using Contexts and Dependency Injection for the Java EE ...
CDI ensures type-safe injection of beans by selecting the bean class on the basis of the Java type that is specified in the...
Read more >
3. The IoC container - Spring
The Spring IoC container can manage virtually any class you want it to manage; it is not limited to managing true JavaBeans. Most...
Read more >
Weld 2.2.16.Final - CDI Reference Implementation
But the specification does not limit the use of CDI to the Java EE environment. ... not concern itself with managing the lifecycle...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found