SEC-2205: Create UserDetailsServiceDelegator
Ensure that the UserDetailsService is created lazily.
This commit is contained in:
@@ -35,7 +35,9 @@ import org.springframework.security.config.annotation.web.configurers.DefaultLog
|
||||
import org.springframework.security.config.annotation.web.configurers.SecurityContextConfigurer;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
|
||||
import org.springframework.web.accept.ContentNegotiationStrategy;
|
||||
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
|
||||
@@ -238,7 +240,7 @@ public abstract class WebSecurityConfigurerAdapter implements SecurityConfigurer
|
||||
* @see {@link #userDetailsService()}
|
||||
*/
|
||||
public UserDetailsService userDetailsServiceBean() throws Exception {
|
||||
return userDetailsService();
|
||||
return new UserDetailsServiceDelegator(parentAuthenticationBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -320,6 +322,41 @@ public abstract class WebSecurityConfigurerAdapter implements SecurityConfigurer
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delays the use of the {@link UserDetailsService} from the
|
||||
* {@link AuthenticationManagerBuilder} to ensure that it has been fully
|
||||
* configured.
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @since 3.2
|
||||
*/
|
||||
static final class UserDetailsServiceDelegator implements UserDetailsService {
|
||||
private AuthenticationManagerBuilder delegateBuilder;
|
||||
private UserDetailsService delegate;
|
||||
private final Object delegateMonitor = new Object();
|
||||
|
||||
UserDetailsServiceDelegator(AuthenticationManagerBuilder authentication) {
|
||||
this.delegateBuilder = authentication;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
if(delegate != null) {
|
||||
return delegate.loadUserByUsername(username);
|
||||
}
|
||||
|
||||
synchronized(delegateMonitor) {
|
||||
if (delegate == null) {
|
||||
delegate = this.delegateBuilder.getDefaultUserDetailsService();
|
||||
this.delegateBuilder = null;
|
||||
}
|
||||
}
|
||||
|
||||
return delegate.loadUserByUsername(username);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delays the use of the {@link AuthenticationManager} build from the
|
||||
* {@link AuthenticationManagerBuilder} to ensure that it has been fully
|
||||
|
||||
Reference in New Issue
Block a user