GlobalMethodSecurityConfiguration uses BeanFactoryAware

This works around an issue found when updating to
Spring 5.1.0.RC2

Issue: gh-5679
This commit is contained in:
Rob Winch 2018-08-17 15:17:38 -05:00
parent 2e620a26de
commit 2c27e18eb3

View File

@ -23,9 +23,12 @@ import org.aopalliance.intercept.MethodInterceptor;
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.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AdviceMode; import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -81,7 +84,7 @@ import org.springframework.util.Assert;
*/ */
@Configuration @Configuration
public class GlobalMethodSecurityConfiguration public class GlobalMethodSecurityConfiguration
implements ImportAware, SmartInitializingSingleton { implements ImportAware, SmartInitializingSingleton, BeanFactoryAware {
private static final Log logger = LogFactory private static final Log logger = LogFactory
.getLog(GlobalMethodSecurityConfiguration.class); .getLog(GlobalMethodSecurityConfiguration.class);
private ObjectPostProcessor<Object> objectPostProcessor = new ObjectPostProcessor<Object>() { private ObjectPostProcessor<Object> objectPostProcessor = new ObjectPostProcessor<Object>() {
@ -96,9 +99,8 @@ public class GlobalMethodSecurityConfiguration
private AuthenticationManagerBuilder auth; private AuthenticationManagerBuilder auth;
private boolean disableAuthenticationRegistry; private boolean disableAuthenticationRegistry;
private AnnotationAttributes enableMethodSecurity; private AnnotationAttributes enableMethodSecurity;
private ApplicationContext context; private BeanFactory context;
private MethodSecurityExpressionHandler expressionHandler; private MethodSecurityExpressionHandler expressionHandler;
private Jsr250MethodSecurityMetadataSource jsr250MethodSecurityMetadataSource;
private MethodSecurityInterceptor methodSecurityInterceptor; private MethodSecurityInterceptor methodSecurityInterceptor;
/** /**
@ -180,12 +182,11 @@ public class GlobalMethodSecurityConfiguration
} }
private <T> T getSingleBeanOrNull(Class<T> type) { private <T> T getSingleBeanOrNull(Class<T> type) {
String[] beanNamesForType = this.context.getBeanNamesForType(type); try {
if (beanNamesForType == null || beanNamesForType.length != 1) { return context.getBean(type);
} catch (NoSuchBeanDefinitionException e) {}
return null; return null;
} }
return this.context.getBean(beanNamesForType[0], type);
}
private void initializeMethodSecurityInterceptor() throws Exception { private void initializeMethodSecurityInterceptor() throws Exception {
if(this.methodSecurityInterceptor == null) { if(this.methodSecurityInterceptor == null) {
@ -366,8 +367,9 @@ public class GlobalMethodSecurityConfiguration
if (jsr250Enabled()) { if (jsr250Enabled()) {
GrantedAuthorityDefaults grantedAuthorityDefaults = GrantedAuthorityDefaults grantedAuthorityDefaults =
getSingleBeanOrNull(GrantedAuthorityDefaults.class); getSingleBeanOrNull(GrantedAuthorityDefaults.class);
Jsr250MethodSecurityMetadataSource jsr250MethodSecurityMetadataSource = this.context.getBean(Jsr250MethodSecurityMetadataSource.class);
if (grantedAuthorityDefaults != null) { if (grantedAuthorityDefaults != null) {
this.jsr250MethodSecurityMetadataSource.setDefaultRolePrefix( jsr250MethodSecurityMetadataSource.setDefaultRolePrefix(
grantedAuthorityDefaults.getRolePrefix()); grantedAuthorityDefaults.getRolePrefix());
} }
sources.add(jsr250MethodSecurityMetadataSource); sources.add(jsr250MethodSecurityMetadataSource);
@ -405,12 +407,6 @@ public class GlobalMethodSecurityConfiguration
.postProcess(defaultMethodExpressionHandler); .postProcess(defaultMethodExpressionHandler);
} }
@Autowired(required = false)
public void setJsr250MethodSecurityMetadataSource(
Jsr250MethodSecurityMetadataSource jsr250MethodSecurityMetadataSource) {
this.jsr250MethodSecurityMetadataSource = jsr250MethodSecurityMetadataSource;
}
@Autowired(required = false) @Autowired(required = false)
public void setMethodSecurityExpressionHandler( public void setMethodSecurityExpressionHandler(
List<MethodSecurityExpressionHandler> handlers) { List<MethodSecurityExpressionHandler> handlers) {
@ -422,9 +418,9 @@ public class GlobalMethodSecurityConfiguration
this.expressionHandler = handlers.get(0); this.expressionHandler = handlers.get(0);
} }
@Autowired @Override
public void setApplicationContext(ApplicationContext context) { public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.context = context; this.context = beanFactory;
} }
private AuthenticationConfiguration getAuthenticationConfiguration() { private AuthenticationConfiguration getAuthenticationConfiguration() {