WithUserDetails supports ReactiveUserDetailsService
Fixes: gh-4888
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user