@Bean public ReactiveUserDetailsService userDetailsRepository() { return new ReactiveUserDetailsService() { @Override public Mono<UserDetails> findByUsername(String username) { Query query = new Query(); query.addCriteria(Criteria.where("userId").is(username)); return operations.findOne(query, UserDetails.class); } }; }
@Test public void configuresADefaultUser() { contextRunner .withPropertyValues("singleuser.name=michael") .withConfiguration(AutoConfigurations.of(SecurityAutoConfiguration.class, SingleUserAutoConfiguration.class)) .run(context -> { assertThat(context.getBean(UserDetailsService.class).loadUserByUsername("michael")).isNotNull(); assertThat(context).getBean(ReactiveUserDetailsService.class).isNull(); }); }
@Test public void configuresADefaultUser() { contextRunner .withUserConfiguration(TestConfig.class) .withPropertyValues("singleuser.name=michael") .withConfiguration(AutoConfigurations.of(ReactiveSecurityAutoConfiguration.class, SingleUserAutoConfiguration.class)) .run(context -> { assertThat(context.getBean(ReactiveUserDetailsService.class).findByUsername("michael").block()).isNotNull(); assertThat(context).getBean(UserDetailsService.class).isNull(); }); }
@Test public void configuresNoDefaultUserWhenUserDetailsServicePresent() { contextRunner .withUserConfiguration(TestConfig.class, UserDetailsRepositoryIsPresent.class) .withConfiguration(AutoConfigurations.of(ReactiveSecurityAutoConfiguration.class, SingleUserAutoConfiguration.class)) .run(context -> { assertThat(context.getBean(ReactiveUserDetailsService.class).findByUsername("bob").block()).isNotNull(); assertThat(context.getBean(ReactiveUserDetailsService.class).findByUsername("michael").block()).isNull(); assertThat(context).getBean(UserDetailsService.class).isNull(); }); }
@Bean public ReactiveUserDetailsService userDetailsService(UserRepository users) { return (username) -> users.findByUsername(username).cast(UserDetails.class); }
@Bean public ReactiveUserDetailsService singleUserDetailsService( final SingleUserProperties singleUserProperties ) { return new MapReactiveUserDetailsService(singleUserProperties.asUser()); }
@Bean public ReactiveUserDetailsService userDetailsService() { return new MapReactiveUserDetailsService(User.withUsername("Bob").password("bobby").roles("USER").build()); }