Disable bean proxying in configuration classes

Fixes gh-6967
This commit is contained in:
Eleftheria Stein 2019-06-10 12:14:56 -04:00 committed by Rob Winch
parent 371a3b9c7f
commit 1ec040e554
20 changed files with 234 additions and 34 deletions

View File

@ -57,7 +57,7 @@ import java.util.stream.Collectors;
* @since 3.2 * @since 3.2
* *
*/ */
@Configuration @Configuration(proxyBeanMethods = false)
@Import(ObjectPostProcessorConfiguration.class) @Import(ObjectPostProcessorConfiguration.class)
public class AuthenticationConfiguration { public class AuthenticationConfiguration {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,7 +33,7 @@ import org.springframework.security.config.annotation.web.configuration.EnableWe
* @author Rob Winch * @author Rob Winch
* @since 3.2 * @since 3.2
*/ */
@Configuration @Configuration(proxyBeanMethods = false)
public class ObjectPostProcessorConfiguration { public class ObjectPostProcessorConfiguration {
@Bean @Bean

View File

@ -79,7 +79,7 @@ import org.springframework.util.Assert;
* @since 3.2 * @since 3.2
* @see EnableGlobalMethodSecurity * @see EnableGlobalMethodSecurity
*/ */
@Configuration @Configuration(proxyBeanMethods = false)
public class GlobalMethodSecurityConfiguration public class GlobalMethodSecurityConfiguration
implements ImportAware, SmartInitializingSingleton, BeanFactoryAware { implements ImportAware, SmartInitializingSingleton, BeanFactoryAware {
private static final Log logger = LogFactory private static final Log logger = LogFactory

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -19,7 +19,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.annotation.Jsr250MethodSecurityMetadataSource; import org.springframework.security.access.annotation.Jsr250MethodSecurityMetadataSource;
@Configuration @Configuration(proxyBeanMethods = false)
class Jsr250MetadataSourceConfiguration { class Jsr250MetadataSourceConfiguration {
@Bean @Bean

View File

@ -38,7 +38,7 @@ import java.util.Arrays;
* @author Tadaya Tsuyukubo * @author Tadaya Tsuyukubo
* @since 5.0 * @since 5.0
*/ */
@Configuration @Configuration(proxyBeanMethods = false)
class ReactiveMethodSecurityConfiguration implements ImportAware { class ReactiveMethodSecurityConfiguration implements ImportAware {
private int advisorOrder; private int advisorOrder;

View File

@ -58,7 +58,7 @@ final class OAuth2ClientConfiguration {
} }
} }
@Configuration @Configuration(proxyBeanMethods = false)
static class OAuth2ClientWebMvcSecurityConfiguration implements WebMvcConfigurer { static class OAuth2ClientWebMvcSecurityConfiguration implements WebMvcConfigurer {
private ClientRegistrationRepository clientRegistrationRepository; private ClientRegistrationRepository clientRegistrationRepository;
private OAuth2AuthorizedClientRepository authorizedClientRepository; private OAuth2AuthorizedClientRepository authorizedClientRepository;

View File

@ -63,7 +63,7 @@ import org.springframework.security.web.context.AbstractSecurityWebApplicationIn
* @author Keesun Baik * @author Keesun Baik
* @since 3.2 * @since 3.2
*/ */
@Configuration @Configuration(proxyBeanMethods = false)
public class WebSecurityConfiguration implements ImportAware, BeanClassLoaderAware { public class WebSecurityConfiguration implements ImportAware, BeanClassLoaderAware {
private WebSecurity webSecurity; private WebSecurity webSecurity;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -52,7 +52,7 @@ final class ReactiveOAuth2ClientImportSelector implements ImportSelector {
new String[] {}; new String[] {};
} }
@Configuration @Configuration(proxyBeanMethods = false)
static class OAuth2ClientWebFluxSecurityConfiguration implements WebFluxConfigurer { static class OAuth2ClientWebFluxSecurityConfiguration implements WebFluxConfigurer {
private ReactiveClientRegistrationRepository clientRegistrationRepository; private ReactiveClientRegistrationRepository clientRegistrationRepository;

View File

@ -42,7 +42,7 @@ import org.springframework.web.reactive.result.method.annotation.ArgumentResolve
* @author Dan Zheng * @author Dan Zheng
* @since 5.0 * @since 5.0
*/ */
@Configuration @Configuration(proxyBeanMethods = false)
class ServerHttpSecurityConfiguration { class ServerHttpSecurityConfiguration {
private static final String BEAN_NAME_PREFIX = "org.springframework.security.config.annotation.web.reactive.HttpSecurityConfiguration."; private static final String BEAN_NAME_PREFIX = "org.springframework.security.config.annotation.web.reactive.HttpSecurityConfiguration.";
private static final String HTTPSECURITY_BEAN_NAME = BEAN_NAME_PREFIX + "httpSecurity"; private static final String HTTPSECURITY_BEAN_NAME = BEAN_NAME_PREFIX + "httpSecurity";

View File

@ -38,7 +38,7 @@ import org.springframework.web.reactive.result.view.AbstractView;
* @author Rob Winch * @author Rob Winch
* @since 5.0 * @since 5.0
*/ */
@Configuration @Configuration(proxyBeanMethods = false)
class WebFluxSecurityConfiguration { class WebFluxSecurityConfiguration {
public static final int WEB_FILTER_CHAIN_FILTER_ORDER = 0 - 100; public static final int WEB_FILTER_CHAIN_FILTER_ORDER = 0 - 100;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,6 +18,7 @@ package org.springframework.security.config.annotation.web.servlet.configuration
import java.util.List; import java.util.List;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver; import org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver;
import org.springframework.security.web.servlet.support.csrf.CsrfRequestDataValueProcessor; import org.springframework.security.web.servlet.support.csrf.CsrfRequestDataValueProcessor;
@ -37,6 +38,7 @@ import org.springframework.web.servlet.support.RequestDataValueProcessor;
* @author Rob Winch * @author Rob Winch
* @since 3.2 * @since 3.2
*/ */
@Configuration(proxyBeanMethods = false)
@EnableWebSecurity @EnableWebSecurity
public class WebMvcSecurityConfiguration implements WebMvcConfigurer { public class WebMvcSecurityConfiguration implements WebMvcConfigurer {

View File

@ -542,7 +542,7 @@ public class AuthenticationConfigurationTests {
.isInstanceOf(AlreadyBuiltException.class); .isInstanceOf(AlreadyBuiltException.class);
} }
@Configuration(proxyBeanMethods = false) @Configuration
static class AuthenticationConfigurationSubclass extends AuthenticationConfiguration { static class AuthenticationConfigurationSubclass extends AuthenticationConfiguration {
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,26 +17,30 @@ package org.springframework.security.config.annotation.authentication.configurat
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.security.config.test.SpringTestRule;
/** /**
* *
* @author Rob Winch * @author Rob Winch
* *
*/ */
@RunWith(SpringJUnit4ClassRunner.class)
public class EnableGlobalAuthenticationTests { public class EnableGlobalAuthenticationTests {
@Autowired @Rule
AuthenticationConfiguration auth; public final SpringTestRule spring = new SpringTestRule();
// gh-4086 // gh-4086
@Test @Test
public void authenticationConfigurationWhenGetAuthenticationManagerThenNotNull() throws Exception { public void authenticationConfigurationWhenGetAuthenticationManagerThenNotNull() throws Exception {
this.spring.register(Config.class).autowire();
AuthenticationConfiguration auth = spring.getContext().getBean(AuthenticationConfiguration.class);
assertThat(auth.getAuthenticationManager()).isNotNull(); assertThat(auth.getAuthenticationManager()).isNotNull();
} }
@ -50,4 +54,67 @@ public class EnableGlobalAuthenticationTests {
} }
} }
@Test
public void enableGlobalAuthenticationWhenNoConfigurationAnnotationThenBeanProxyingEnabled() {
this.spring.register(BeanProxyEnabledByDefaultConfig.class).autowire();
Child childBean = this.spring.getContext().getBean(Child.class);
Parent parentBean = this.spring.getContext().getBean(Parent.class);
assertThat(parentBean.getChild()).isSameAs(childBean);
}
@EnableGlobalAuthentication
static class BeanProxyEnabledByDefaultConfig {
@Bean
public Child child() {
return new Child();
}
@Bean
public Parent parent() {
return new Parent(child());
}
}
@Test
public void enableGlobalAuthenticationWhenProxyBeanMethodsFalseThenBeanProxyingDisabled() {
this.spring.register(BeanProxyDisabledConfig.class).autowire();
Child childBean = this.spring.getContext().getBean(Child.class);
Parent parentBean = this.spring.getContext().getBean(Parent.class);
assertThat(parentBean.getChild()).isNotSameAs(childBean);
}
@Configuration(proxyBeanMethods = false)
@EnableGlobalAuthentication
static class BeanProxyDisabledConfig {
@Bean
public Child child() {
return new Child();
}
@Bean
public Parent parent() {
return new Parent(child());
}
}
static class Parent {
private Child child;
Parent(Child child) {
this.child = child;
}
public Child getChild() {
return child;
}
}
static class Child {
Child() {
}
}
} }

View File

@ -557,7 +557,7 @@ public class GlobalMethodSecurityConfigurationTests {
@Test @Test
public void methodSecurityInterceptorUsesMetadataSourceBeanWhenProxyingDisabled() { public void methodSecurityInterceptorUsesMetadataSourceBeanWhenProxyingDisabled() {
this.spring.register(CustomMetadataSourceProxylessConfig.class).autowire(); this.spring.register(CustomMetadataSourceBeanProxyEnabledConfig.class).autowire();
MethodSecurityInterceptor methodInterceptor = MethodSecurityInterceptor methodInterceptor =
(MethodSecurityInterceptor) this.spring.getContext().getBean(MethodInterceptor.class); (MethodSecurityInterceptor) this.spring.getContext().getBean(MethodInterceptor.class);
MethodSecurityMetadataSource methodSecurityMetadataSource = MethodSecurityMetadataSource methodSecurityMetadataSource =
@ -567,7 +567,7 @@ public class GlobalMethodSecurityConfigurationTests {
} }
@EnableGlobalMethodSecurity(prePostEnabled = true) @EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration(proxyBeanMethods = false) @Configuration
public static class CustomMetadataSourceProxylessConfig extends GlobalMethodSecurityConfiguration { public static class CustomMetadataSourceBeanProxyEnabledConfig extends GlobalMethodSecurityConfiguration {
} }
} }

View File

@ -89,7 +89,7 @@ public class ReactiveMethodSecurityConfigurationTests {
} }
@Test @Test
public void rolePrefixWithGrantedAuthorityDefaultsAndSubclassWithProxyingDisabled() { public void rolePrefixWithGrantedAuthorityDefaultsAndSubclassWithProxyingEnabled() {
this.spring.register(SubclassConfig.class).autowire(); this.spring.register(SubclassConfig.class).autowire();
TestingAuthenticationToken authentication = new TestingAuthenticationToken( TestingAuthenticationToken authentication = new TestingAuthenticationToken(
@ -105,7 +105,7 @@ public class ReactiveMethodSecurityConfigurationTests {
assertThat(root.hasRole("ABC")).isTrue(); assertThat(root.hasRole("ABC")).isTrue();
} }
@Configuration(proxyBeanMethods = false) @Configuration
static class SubclassConfig extends ReactiveMethodSecurityConfiguration { static class SubclassConfig extends ReactiveMethodSecurityConfiguration {
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -122,4 +122,68 @@ public class EnableWebSecurityTests {
} }
} }
} }
@Test
public void enableWebSecurityWhenNoConfigurationAnnotationThenBeanProxyingEnabled() {
this.spring.register(BeanProxyEnabledByDefaultConfig.class).autowire();
Child childBean = this.spring.getContext().getBean(Child.class);
Parent parentBean = this.spring.getContext().getBean(Parent.class);
assertThat(parentBean.getChild()).isSameAs(childBean);
}
@EnableWebSecurity
static class BeanProxyEnabledByDefaultConfig extends WebSecurityConfigurerAdapter {
@Bean
public Child child() {
return new Child();
}
@Bean
public Parent parent() {
return new Parent(child());
}
}
@Test
public void enableWebSecurityWhenProxyBeanMethodsFalseThenBeanProxyingDisabled() {
this.spring.register(BeanProxyDisabledConfig.class).autowire();
Child childBean = this.spring.getContext().getBean(Child.class);
Parent parentBean = this.spring.getContext().getBean(Parent.class);
assertThat(parentBean.getChild()).isNotSameAs(childBean);
}
@Configuration(proxyBeanMethods = false)
@EnableWebSecurity
static class BeanProxyDisabledConfig extends WebSecurityConfigurerAdapter {
@Bean
public Child child() {
return new Child();
}
@Bean
public Parent parent() {
return new Parent(child());
}
}
static class Parent {
private Child child;
Parent(Child child) {
this.child = child;
}
public Child getChild() {
return child;
}
}
static class Child {
Child() {
}
}
} }

View File

@ -406,7 +406,7 @@ public class WebSecurityConfigurationTests {
} }
@Test @Test
public void loadConfigWhenProxyingDisabledAndSubclassThenFilterChainsCreated() { public void loadConfigWhenBeanProxyingEnabledAndSubclassThenFilterChainsCreated() {
this.spring.register(GlobalAuthenticationWebSecurityConfigurerAdaptersConfig.class, SubclassConfig.class).autowire(); this.spring.register(GlobalAuthenticationWebSecurityConfigurerAdaptersConfig.class, SubclassConfig.class).autowire();
FilterChainProxy filterChainProxy = this.spring.getContext().getBean(FilterChainProxy.class); FilterChainProxy filterChainProxy = this.spring.getContext().getBean(FilterChainProxy.class);
@ -415,7 +415,7 @@ public class WebSecurityConfigurationTests {
assertThat(filterChains).hasSize(4); assertThat(filterChains).hasSize(4);
} }
@Configuration(proxyBeanMethods = false) @Configuration
static class SubclassConfig extends WebSecurityConfiguration { static class SubclassConfig extends WebSecurityConfiguration {
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
@ -367,4 +368,70 @@ public class EnableWebFluxSecurityTests {
buffer.write(body.getBytes(StandardCharsets.UTF_8)); buffer.write(body.getBytes(StandardCharsets.UTF_8));
return buffer; return buffer;
} }
@Test
public void enableWebFluxSecurityWhenNoConfigurationAnnotationThenBeanProxyingEnabled() {
this.spring.register(BeanProxyEnabledByDefaultConfig.class).autowire();
Child childBean = this.spring.getContext().getBean(Child.class);
Parent parentBean = this.spring.getContext().getBean(Parent.class);
assertThat(parentBean.getChild()).isSameAs(childBean);
}
@EnableWebFluxSecurity
@Import(ReactiveAuthenticationTestConfiguration.class)
static class BeanProxyEnabledByDefaultConfig {
@Bean
public Child child() {
return new Child();
}
@Bean
public Parent parent() {
return new Parent(child());
}
}
@Test
public void enableWebFluxSecurityWhenProxyBeanMethodsFalseThenBeanProxyingDisabled() {
this.spring.register(BeanProxyDisabledConfig.class).autowire();
Child childBean = this.spring.getContext().getBean(Child.class);
Parent parentBean = this.spring.getContext().getBean(Parent.class);
assertThat(parentBean.getChild()).isNotSameAs(childBean);
}
@Configuration(proxyBeanMethods = false)
@EnableWebFluxSecurity
@Import(ReactiveAuthenticationTestConfiguration.class)
static class BeanProxyDisabledConfig {
@Bean
public Child child() {
return new Child();
}
@Bean
public Parent parent() {
return new Parent(child());
}
}
static class Parent {
private Child child;
Parent(Child child) {
this.child = child;
}
public Child getChild() {
return child;
}
}
static class Child {
Child() {
}
}
} }

View File

@ -44,7 +44,7 @@ public class ServerHttpSecurityConfigurationTest {
} }
@Test @Test
public void loadConfigWhenProxyingDisabledAndSubclassThenServerHttpSecurityExists() { public void loadConfigWhenProxyingEnabledAndSubclassThenServerHttpSecurityExists() {
this.spring.register(SubclassConfig.class, ReactiveAuthenticationTestConfiguration.class, this.spring.register(SubclassConfig.class, ReactiveAuthenticationTestConfiguration.class,
WebFluxSecurityConfiguration.class).autowire(); WebFluxSecurityConfiguration.class).autowire();
ServerHttpSecurity serverHttpSecurity = this.spring.getContext().getBean(ServerHttpSecurity.class); ServerHttpSecurity serverHttpSecurity = this.spring.getContext().getBean(ServerHttpSecurity.class);
@ -52,7 +52,7 @@ public class ServerHttpSecurityConfigurationTest {
assertThat(serverHttpSecurity).isNotNull(); assertThat(serverHttpSecurity).isNotNull();
} }
@Configuration(proxyBeanMethods = false) @Configuration
static class SubclassConfig extends ServerHttpSecurityConfiguration { static class SubclassConfig extends ServerHttpSecurityConfiguration {
} }
} }

View File

@ -44,7 +44,7 @@ public class WebFluxSecurityConfigurationTests {
} }
@Test @Test
public void loadConfigWhenProxyingDisabledAndSubclassThenWebFilterChainProxyExists() { public void loadConfigWhenBeanProxyingEnabledAndSubclassThenWebFilterChainProxyExists() {
this.spring.register(ServerHttpSecurityConfiguration.class, ReactiveAuthenticationTestConfiguration.class, this.spring.register(ServerHttpSecurityConfiguration.class, ReactiveAuthenticationTestConfiguration.class,
WebFluxSecurityConfigurationTests.SubclassConfig.class).autowire(); WebFluxSecurityConfigurationTests.SubclassConfig.class).autowire();
WebFilterChainProxy webFilterChainProxy = this.spring.getContext().getBean(WebFilterChainProxy.class); WebFilterChainProxy webFilterChainProxy = this.spring.getContext().getBean(WebFilterChainProxy.class);
@ -52,7 +52,7 @@ public class WebFluxSecurityConfigurationTests {
assertThat(webFilterChainProxy).isNotNull(); assertThat(webFilterChainProxy).isNotNull();
} }
@Configuration(proxyBeanMethods = false) @Configuration
static class SubclassConfig extends WebFluxSecurityConfiguration { static class SubclassConfig extends WebFluxSecurityConfiguration {
} }
} }