WithUserDetails supports ReactiveUserDetailsService

Fixes: gh-4888
This commit is contained in:
Rob Winch 2018-03-08 23:13:19 -06:00
parent 949c7d68b8
commit 2228485a40
2 changed files with 95 additions and 10 deletions

View File

@ -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<WithUserDetails> {
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;
}
}
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();
}
}
}

View File

@ -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);
}
}