Skip to content

Conversation

siva-sai-udaygiri
Copy link

Motivation

In scenarios where multiple UserDetailsService or UserDetailsPasswordService beans
are defined, Spring may raise NoUniqueBeanDefinitionException or select the wrong
bean without explicit resolution.

Changes

  • Added @Primary to UserDetailsService and UserDetailsPasswordService beans in
    InitializeUserDetailsBeanManagerConfigurer
  • Ensures deterministic bean selection

Issue Reference

Fixes gh-17902

Testing

  • Added a test in InitializeUserDetailsBeanManagerConfigurerTests to validate
    resolution when multiple candidates are present
  • Verified that all existing tests pass with ./gradlew check

Notes

  • No breaking changes
  • Consistent with Spring idioms for bean resolution

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 6, 2025
@jzheaux jzheaux force-pushed the honor-primary-uds-17902 branch from 2382273 to d4e00e4 Compare October 20, 2025 21:16
@jzheaux jzheaux self-assigned this Oct 20, 2025
@jzheaux jzheaux added in: config An issue in spring-security-config type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 20, 2025
Copy link
Contributor

@jzheaux jzheaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @siva-sai-udaygirl, for the PR! I've left some feedback inline.


PasswordEncoder passwordEncoder = getBeanIfUnique(PasswordEncoder.class);
// Also resolve UDPS via container so @Primary is honored
UserDetailsPasswordService passwordManager = getAutowireCandidateOrNull(UserDetailsPasswordService.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this changes the existing semantics by calling getIfAvailable instead of getIfUnique. The ObjectProvider JavaDoc implies that it accounts for @Primary:

	/**
	 * Return an instance (possibly shared or independent) of the object
	 * managed by this factory.
	 * @return an instance of the bean, or {@code null} if not available or
	 * not unique (i.e. multiple candidates found **with none marked as primary**)
	 * @throws BeansException in case of creation errors
	 * @see #getObject()
	 */

Can you confirm with unit tests that changing to getIfAvailable is necessary to support @Primary?

siva-sai-udaygiri and others added 3 commits October 20, 2025 17:08
This commit rearranges the branches to reduce nesting

Signed-off-by: Josh Cummings <[email protected]>
Given that the codebase uses getBeanIfUnique logic in many places,
it is a little noisy to clarify how it works in just this location.

Instead, the previous commit simplifies the branching logic, reducing
the need for clarification of what could otherwise be surprising.

Signed-off-by: Josh Cummings <[email protected]>
@jzheaux jzheaux force-pushed the honor-primary-uds-17902 branch from d4e00e4 to 581d666 Compare October 20, 2025 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: config An issue in spring-security-config type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InitializeUserDetailsBeanManagerConfigurer should respect primary flag

3 participants