|
@@ -19,9 +19,11 @@ import java.util.Arrays;
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.LogFactory;
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
+
|
|
import org.springframework.aop.framework.ProxyFactoryBean;
|
|
import org.springframework.aop.framework.ProxyFactoryBean;
|
|
import org.springframework.aop.target.LazyInitTargetSource;
|
|
import org.springframework.aop.target.LazyInitTargetSource;
|
|
import org.springframework.beans.factory.BeanFactoryUtils;
|
|
import org.springframework.beans.factory.BeanFactoryUtils;
|
|
@@ -34,6 +36,8 @@ import org.springframework.security.authentication.AuthenticationManager;
|
|
import org.springframework.security.config.annotation.ObjectPostProcessor;
|
|
import org.springframework.security.config.annotation.ObjectPostProcessor;
|
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
|
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
|
|
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
|
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
|
+import org.springframework.security.core.AuthenticationException;
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -45,6 +49,9 @@ import org.springframework.util.Assert;
|
|
*/
|
|
*/
|
|
@Configuration
|
|
@Configuration
|
|
public class AuthenticationConfiguration {
|
|
public class AuthenticationConfiguration {
|
|
|
|
+
|
|
|
|
+ private AtomicBoolean buildingAuthenticationManager = new AtomicBoolean();
|
|
|
|
+
|
|
private ApplicationContext applicationContext;
|
|
private ApplicationContext applicationContext;
|
|
|
|
|
|
private AuthenticationManager authenticationManager;
|
|
private AuthenticationManager authenticationManager;
|
|
@@ -79,11 +86,15 @@ public class AuthenticationConfiguration {
|
|
}
|
|
}
|
|
|
|
|
|
public AuthenticationManager getAuthenticationManager() throws Exception {
|
|
public AuthenticationManager getAuthenticationManager() throws Exception {
|
|
- if (authenticationManagerInitialized) {
|
|
|
|
- return authenticationManager;
|
|
|
|
|
|
+ if (this.authenticationManagerInitialized) {
|
|
|
|
+ return this.authenticationManager;
|
|
|
|
+ }
|
|
|
|
+ AuthenticationManagerBuilder authBuilder = authenticationManagerBuilder(
|
|
|
|
+ this.objectPostProcessor);
|
|
|
|
+ if (this.buildingAuthenticationManager.getAndSet(true)) {
|
|
|
|
+ return new AuthenticationManagerDelegator(authBuilder);
|
|
}
|
|
}
|
|
|
|
|
|
- AuthenticationManagerBuilder authBuilder = authenticationManagerBuilder(objectPostProcessor);
|
|
|
|
for (GlobalAuthenticationConfigurerAdapter config : globalAuthConfigurers) {
|
|
for (GlobalAuthenticationConfigurerAdapter config : globalAuthConfigurers) {
|
|
authBuilder.apply(config);
|
|
authBuilder.apply(config);
|
|
}
|
|
}
|
|
@@ -157,4 +168,44 @@ public class AuthenticationConfiguration {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Prevents infinite recursion in the event that initializing the
|
|
|
|
+ * AuthenticationManager.
|
|
|
|
+ *
|
|
|
|
+ * @author Rob Winch
|
|
|
|
+ * @since 4.1.1
|
|
|
|
+ */
|
|
|
|
+ static final class AuthenticationManagerDelegator implements AuthenticationManager {
|
|
|
|
+ private AuthenticationManagerBuilder delegateBuilder;
|
|
|
|
+ private AuthenticationManager delegate;
|
|
|
|
+ private final Object delegateMonitor = new Object();
|
|
|
|
+
|
|
|
|
+ AuthenticationManagerDelegator(AuthenticationManagerBuilder delegateBuilder) {
|
|
|
|
+ Assert.notNull(delegateBuilder, "delegateBuilder cannot be null");
|
|
|
|
+ this.delegateBuilder = delegateBuilder;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public Authentication authenticate(Authentication authentication)
|
|
|
|
+ throws AuthenticationException {
|
|
|
|
+ if (this.delegate != null) {
|
|
|
|
+ return this.delegate.authenticate(authentication);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ synchronized (this.delegateMonitor) {
|
|
|
|
+ if (this.delegate == null) {
|
|
|
|
+ this.delegate = this.delegateBuilder.getObject();
|
|
|
|
+ this.delegateBuilder = null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return this.delegate.authenticate(authentication);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String toString() {
|
|
|
|
+ return "AuthenticationManagerDelegator [delegate=" + this.delegate + "]";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|