From 2228485a40cf0f4bc8d3ad1a5d366b39bbdc92da Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Thu, 8 Mar 2018 23:13:19 -0600 Subject: [PATCH] WithUserDetails supports ReactiveUserDetailsService Fixes: gh-4888 --- ...WithUserDetailsSecurityContextFactory.java | 54 +++++++++++++++++-- ...serDetailsSecurityContextFactoryTests.java | 51 ++++++++++++++++-- 2 files changed, 95 insertions(+), 10 deletions(-) diff --git a/test/src/main/java/org/springframework/security/test/context/support/WithUserDetailsSecurityContextFactory.java b/test/src/main/java/org/springframework/security/test/context/support/WithUserDetailsSecurityContextFactory.java index 971182b142..d03fa90419 100644 --- a/test/src/main/java/org/springframework/security/test/context/support/WithUserDetailsSecurityContextFactory.java +++ b/test/src/main/java/org/springframework/security/test/context/support/WithUserDetailsSecurityContextFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,19 @@ package org.springframework.security.test.context.support; import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanNotOfRequiredTypeException; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.ReactiveUserDetailsService; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; /** @@ -39,6 +44,8 @@ import org.springframework.util.StringUtils; final class WithUserDetailsSecurityContextFactory implements WithSecurityContextFactory { + private static final boolean reactorPresent = ClassUtils.isPresent("reactor.core.publisher.Mono", WithUserDetailsSecurityContextFactory.class.getClassLoader()); + private BeanFactory beans; @Autowired @@ -48,9 +55,7 @@ final class WithUserDetailsSecurityContextFactory implements public SecurityContext createSecurityContext(WithUserDetails withUser) { String beanName = withUser.userDetailsServiceBeanName(); - UserDetailsService userDetailsService = StringUtils.hasLength(beanName) - ? this.beans.getBean(beanName, UserDetailsService.class) - : this.beans.getBean(UserDetailsService.class); + UserDetailsService userDetailsService = findUserDetailsService(beanName); String username = withUser.value(); Assert.hasLength(username, "value() must be non empty String"); UserDetails principal = userDetailsService.loadUserByUsername(username); @@ -60,4 +65,43 @@ final class WithUserDetailsSecurityContextFactory implements context.setAuthentication(authentication); return context; } -} \ No newline at end of file + + private UserDetailsService findUserDetailsService(String beanName) { + if(reactorPresent) { + UserDetailsService reactive = findAndAdaptReactiveUserDetailsService(beanName); + if (reactive != null) { + return reactive; + } + } + return StringUtils.hasLength(beanName) + ? this.beans.getBean(beanName, UserDetailsService.class) + : this.beans.getBean(UserDetailsService.class); + } + + public UserDetailsService findAndAdaptReactiveUserDetailsService(String beanName) { + try { + ReactiveUserDetailsService reactiveUserDetailsService = StringUtils + .hasLength(beanName) ? + this.beans.getBean(beanName, ReactiveUserDetailsService.class) : + this.beans.getBean(ReactiveUserDetailsService.class); + return new ReactiveUserDetailsServiceAdapter(reactiveUserDetailsService); + } catch(NoSuchBeanDefinitionException | BeanNotOfRequiredTypeException notReactive) { + return null; + } + } + + private class ReactiveUserDetailsServiceAdapter implements UserDetailsService { + private final ReactiveUserDetailsService userDetailsService; + + private ReactiveUserDetailsServiceAdapter( + ReactiveUserDetailsService userDetailsService) { + this.userDetailsService = userDetailsService; + } + + @Override + public UserDetails loadUserByUsername(String username) + throws UsernameNotFoundException { + return this.userDetailsService.findByUsername(username).block(); + } + } +} diff --git a/test/src/test/java/org/springframework/security/test/context/support/WithUserDetailsSecurityContextFactoryTests.java b/test/src/test/java/org/springframework/security/test/context/support/WithUserDetailsSecurityContextFactoryTests.java index a58dcd1651..a8629ed038 100644 --- a/test/src/test/java/org/springframework/security/test/context/support/WithUserDetailsSecurityContextFactoryTests.java +++ b/test/src/test/java/org/springframework/security/test/context/support/WithUserDetailsSecurityContextFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,10 @@ package org.springframework.security.test.context.support; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import org.junit.Before; import org.junit.Test; @@ -24,14 +27,21 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanNotOfRequiredTypeException; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.userdetails.ReactiveUserDetailsService; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; +import reactor.core.publisher.Mono; + @RunWith(MockitoJUnitRunner.class) public class WithUserDetailsSecurityContextFactoryTests { + @Mock + private ReactiveUserDetailsService reactiveUserDetailsService; @Mock private UserDetailsService userDetailsService; @Mock @@ -46,7 +56,6 @@ public class WithUserDetailsSecurityContextFactoryTests { @Before public void setup() { - when(beans.getBean(UserDetailsService.class)).thenReturn(userDetailsService); factory = new WithUserDetailsSecurityContextFactory(beans); } @@ -57,6 +66,7 @@ public class WithUserDetailsSecurityContextFactoryTests { @Test(expected = IllegalArgumentException.class) public void createSecurityContextEmptyValue() { + when(withUserDetails.value()).thenReturn(""); factory.createSecurityContext(withUserDetails); } @@ -64,6 +74,8 @@ public class WithUserDetailsSecurityContextFactoryTests { @Test public void createSecurityContextWithExistingUser() { String username = "user"; + when(this.beans.getBean(ReactiveUserDetailsService.class)).thenThrow(new NoSuchBeanDefinitionException("")); + when(beans.getBean(UserDetailsService.class)).thenReturn(userDetailsService); when(withUserDetails.value()).thenReturn(username); when(userDetailsService.loadUserByUsername(username)).thenReturn(userDetails); @@ -72,7 +84,6 @@ public class WithUserDetailsSecurityContextFactoryTests { UsernamePasswordAuthenticationToken.class); assertThat(context.getAuthentication().getPrincipal()).isEqualTo(userDetails); verify(beans).getBean(UserDetailsService.class); - verifyNoMoreInteractions(beans); } // gh-3346 @@ -80,6 +91,7 @@ public class WithUserDetailsSecurityContextFactoryTests { public void createSecurityContextWithUserDetailsServiceName() { String beanName = "secondUserDetailsServiceBean"; String username = "user"; + when(this.beans.getBean(beanName, ReactiveUserDetailsService.class)).thenThrow(new BeanNotOfRequiredTypeException("", ReactiveUserDetailsService.class, UserDetailsService.class)); when(withUserDetails.value()).thenReturn(username); when(withUserDetails.userDetailsServiceBeanName()).thenReturn(beanName); when(userDetailsService.loadUserByUsername(username)).thenReturn(userDetails); @@ -90,6 +102,35 @@ public class WithUserDetailsSecurityContextFactoryTests { UsernamePasswordAuthenticationToken.class); assertThat(context.getAuthentication().getPrincipal()).isEqualTo(userDetails); verify(beans).getBean(beanName, UserDetailsService.class); - verifyNoMoreInteractions(beans); + } + + @Test + public void createSecurityContextWithReactiveUserDetailsService() { + String username = "user"; + when(withUserDetails.value()).thenReturn(username); + when(this.beans.getBean(ReactiveUserDetailsService.class)).thenReturn(this.reactiveUserDetailsService); + when(this.reactiveUserDetailsService.findByUsername(username)).thenReturn(Mono.just(userDetails)); + + SecurityContext context = factory.createSecurityContext(withUserDetails); + assertThat(context.getAuthentication()).isInstanceOf( + UsernamePasswordAuthenticationToken.class); + assertThat(context.getAuthentication().getPrincipal()).isEqualTo(userDetails); + verify(this.beans).getBean(ReactiveUserDetailsService.class); + } + + @Test + public void createSecurityContextWithReactiveUserDetailsServiceAndBeanName() { + String beanName = "secondUserDetailsServiceBean"; + String username = "user"; + when(withUserDetails.value()).thenReturn(username); + when(withUserDetails.userDetailsServiceBeanName()).thenReturn(beanName); + when(this.beans.getBean(beanName, ReactiveUserDetailsService.class)).thenReturn(this.reactiveUserDetailsService); + when(this.reactiveUserDetailsService.findByUsername(username)).thenReturn(Mono.just(userDetails)); + + SecurityContext context = factory.createSecurityContext(withUserDetails); + assertThat(context.getAuthentication()).isInstanceOf( + UsernamePasswordAuthenticationToken.class); + assertThat(context.getAuthentication().getPrincipal()).isEqualTo(userDetails); + verify(this.beans).getBean(beanName, ReactiveUserDetailsService.class); } }