|
| 1 | +/* |
| 2 | + * Copyright 2004-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.config.annotation.authentication.configuration; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import org.springframework.beans.factory.ObjectProvider; |
| 24 | +import org.springframework.context.ApplicationContext; |
| 25 | +import org.springframework.security.authentication.AuthenticationManager; |
| 26 | +import org.springframework.security.authentication.ProviderManager; |
| 27 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 28 | +import org.springframework.security.authentication.password.CompromisedPasswordChecker; |
| 29 | +import org.springframework.security.config.ObjectPostProcessor; |
| 30 | +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 31 | +import org.springframework.security.core.userdetails.User; |
| 32 | +import org.springframework.security.core.userdetails.UserDetailsPasswordService; |
| 33 | +import org.springframework.security.core.userdetails.UserDetailsService; |
| 34 | +import org.springframework.security.crypto.factory.PasswordEncoderFactories; |
| 35 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 36 | +import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
| 37 | + |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; |
| 39 | +import static org.mockito.BDDMockito.given; |
| 40 | +import static org.mockito.Mockito.mock; |
| 41 | + |
| 42 | +class InitializeUserDetailsBeanManagerConfigurerTests { |
| 43 | + |
| 44 | + private static ObjectPostProcessor<Object> opp() { |
| 45 | + return new ObjectPostProcessor<>() { |
| 46 | + @Override |
| 47 | + public <O> O postProcess(O object) { |
| 48 | + return object; |
| 49 | + } |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + @SuppressWarnings("unchecked") |
| 54 | + @Test |
| 55 | + void whenMultipleUdsAndOneResolvableCandidate_thenPrimaryIsAutoWired() throws Exception { |
| 56 | + ApplicationContext ctx = mock(ApplicationContext.class); |
| 57 | + given(ctx.getBeanNamesForType(UserDetailsService.class)).willReturn(new String[] { "udsA", "udsB" }); |
| 58 | + |
| 59 | + PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder(); |
| 60 | + |
| 61 | + InMemoryUserDetailsManager primary = new InMemoryUserDetailsManager( |
| 62 | + User.withUsername("alice").passwordEncoder(encoder::encode).password("pw").roles("USER").build()); |
| 63 | + InMemoryUserDetailsManager secondary = new InMemoryUserDetailsManager(); |
| 64 | + |
| 65 | + ObjectProvider<UserDetailsService> udsProvider = (ObjectProvider<UserDetailsService>) mock( |
| 66 | + ObjectProvider.class); |
| 67 | + given(ctx.getBeanProvider(UserDetailsService.class)).willReturn(udsProvider); |
| 68 | + given(udsProvider.getIfAvailable()).willReturn(primary); // container picks single |
| 69 | + // candidate |
| 70 | + |
| 71 | + // resolveBeanName(..) path |
| 72 | + given(ctx.getBean("udsA")).willReturn(secondary); |
| 73 | + given(ctx.getBean("udsB")).willReturn(primary); |
| 74 | + |
| 75 | + ObjectProvider<PasswordEncoder> peProvider = (ObjectProvider<PasswordEncoder>) mock(ObjectProvider.class); |
| 76 | + given(ctx.getBeanProvider(PasswordEncoder.class)).willReturn(peProvider); |
| 77 | + given(peProvider.getIfUnique()).willReturn(encoder); |
| 78 | + |
| 79 | + // Stub optional providers to avoid NPEs |
| 80 | + ObjectProvider<UserDetailsPasswordService> udpsProvider = (ObjectProvider<UserDetailsPasswordService>) mock( |
| 81 | + ObjectProvider.class); |
| 82 | + given(ctx.getBeanProvider(UserDetailsPasswordService.class)).willReturn(udpsProvider); |
| 83 | + given(udpsProvider.getIfAvailable()).willReturn(null); |
| 84 | + |
| 85 | + ObjectProvider<CompromisedPasswordChecker> cpcProvider = (ObjectProvider<CompromisedPasswordChecker>) mock( |
| 86 | + ObjectProvider.class); |
| 87 | + given(ctx.getBeanProvider(CompromisedPasswordChecker.class)).willReturn(cpcProvider); |
| 88 | + given(cpcProvider.getIfUnique()).willReturn(null); |
| 89 | + |
| 90 | + AuthenticationManagerBuilder builder = new AuthenticationManagerBuilder(opp()); |
| 91 | + new InitializeUserDetailsBeanManagerConfigurer(ctx).new InitializeUserDetailsManagerConfigurer() |
| 92 | + .configure(builder); |
| 93 | + |
| 94 | + AuthenticationManager manager = builder.build(); |
| 95 | + |
| 96 | + // DaoAuthenticationProvider registered |
| 97 | + assertThat(manager).isInstanceOf(ProviderManager.class); |
| 98 | + List<?> providers = ((ProviderManager) manager).getProviders(); |
| 99 | + assertThat(providers) |
| 100 | + .anySatisfy((p) -> assertThat(p.getClass().getSimpleName()).isEqualTo("DaoAuthenticationProvider")); |
| 101 | + |
| 102 | + // Auth works with the primary UDS + encoder |
| 103 | + var auth = manager.authenticate(new UsernamePasswordAuthenticationToken("alice", "pw")); |
| 104 | + assertThat(auth.isAuthenticated()).isTrue(); |
| 105 | + } |
| 106 | + |
| 107 | + @SuppressWarnings("unchecked") |
| 108 | + @Test |
| 109 | + void whenMultipleUdsAndNoSingleCandidate_thenSkipAutoWiring() throws Exception { |
| 110 | + ApplicationContext ctx = mock(ApplicationContext.class); |
| 111 | + given(ctx.getBeanNamesForType(UserDetailsService.class)).willReturn(new String[] { "udsA", "udsB" }); |
| 112 | + |
| 113 | + ObjectProvider<UserDetailsService> udsProvider = (ObjectProvider<UserDetailsService>) mock( |
| 114 | + ObjectProvider.class); |
| 115 | + given(ctx.getBeanProvider(UserDetailsService.class)).willReturn(udsProvider); |
| 116 | + given(udsProvider.getIfAvailable()).willReturn(null); // ambiguous → no single |
| 117 | + // candidate |
| 118 | + |
| 119 | + // Also stub other providers to null |
| 120 | + ObjectProvider<PasswordEncoder> peProvider = (ObjectProvider<PasswordEncoder>) mock(ObjectProvider.class); |
| 121 | + given(ctx.getBeanProvider(PasswordEncoder.class)).willReturn(peProvider); |
| 122 | + given(peProvider.getIfUnique()).willReturn(null); |
| 123 | + |
| 124 | + ObjectProvider<UserDetailsPasswordService> udpsProvider = (ObjectProvider<UserDetailsPasswordService>) mock( |
| 125 | + ObjectProvider.class); |
| 126 | + given(ctx.getBeanProvider(UserDetailsPasswordService.class)).willReturn(udpsProvider); |
| 127 | + given(udpsProvider.getIfAvailable()).willReturn(null); |
| 128 | + |
| 129 | + ObjectProvider<CompromisedPasswordChecker> cpcProvider = (ObjectProvider<CompromisedPasswordChecker>) mock( |
| 130 | + ObjectProvider.class); |
| 131 | + given(ctx.getBeanProvider(CompromisedPasswordChecker.class)).willReturn(cpcProvider); |
| 132 | + given(cpcProvider.getIfUnique()).willReturn(null); |
| 133 | + |
| 134 | + AuthenticationManagerBuilder builder = new AuthenticationManagerBuilder(opp()); |
| 135 | + new InitializeUserDetailsBeanManagerConfigurer(ctx).new InitializeUserDetailsManagerConfigurer() |
| 136 | + .configure(builder); |
| 137 | + |
| 138 | + AuthenticationManager manager = builder.build(); |
| 139 | + |
| 140 | + // Success condition: nothing auto-registered. |
| 141 | + if (manager == null) { |
| 142 | + assertThat(manager).isNull(); |
| 143 | + } |
| 144 | + else if (manager instanceof ProviderManager pm) { |
| 145 | + assertThat(pm.getProviders()) |
| 146 | + .noneMatch((p) -> p.getClass().getSimpleName().equals("DaoAuthenticationProvider")); |
| 147 | + } |
| 148 | + else { |
| 149 | + assertThat(manager.getClass().getSimpleName()).isNotEqualTo("ProviderManager"); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments