Skip to content

ServletOAuth2AuthorizedClientExchangeFilterFunction doesn't obtain a token if no servlet request #19324

@petergphillips

Description

@petergphillips

Describe the bug
After the upgrade to Spring Security 7.1.0 our web clients no longer attempt to retrieve an oauth2 token when used in batch jobs and when fired from event listeners.

If there is a servlet request (so triggered from an incoming request) then the web clients will retrieve an oauth2 token as expected.

To Reproduce
Create a web client with a ServletOAuth2AuthorizedClientExchangeFilterFunction that is created from a AuthorizedClientServiceOAuth2AuthorizedClientManager. This manager (according to the docs) should be capable of operating in a scheduled / background thread. So:

new ServletOAuth2AuthorizedClientExchangeFilterFunction(
		new AuthorizedClientServiceOAuth2AuthorizedClientManager(this.clientRegistrationRepository,
				new InMemoryOAuth2AuthorizedClientService(this.clientRegistrationRepository)
		)
);

Call the web client without any request / response attributes in thread local scope.

Expected behavior
A token is retrieved from the oauth2 provider for the web client request.

Actual behavior
No token is retrieved at all. Looking at the recent changes to the ServletOAuth2AuthorizedClientExchangeFilterFunction there was a null check that was added in baad23c:

		if (servletRequest == null || servletResponse == null) {
			return Mono.empty();
		}

Our service then reaches that code and then returns, so no token is then retrieved. The web client then fails as no authentication header is set in the call.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions