Skip to content

Commit

Permalink
Disable bean proxying in configuration classes
Browse files Browse the repository at this point in the history
  • Loading branch information
eleftherias authored and kostya05983 committed Aug 26, 2019
1 parent 9acd962 commit a37ce53
Show file tree
Hide file tree
Showing 20 changed files with 234 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
* @since 3.2
*
*/
@Configuration
@Configuration(proxyBeanMethods = false)
@Import(ObjectPostProcessorConfiguration.class)
public class AuthenticationConfiguration {

Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,7 +33,7 @@
* @author Rob Winch
* @since 3.2
*/
@Configuration
@Configuration(proxyBeanMethods = false)
public class ObjectPostProcessorConfiguration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
* @since 3.2
* @see EnableGlobalMethodSecurity
*/
@Configuration
@Configuration(proxyBeanMethods = false)
public class GlobalMethodSecurityConfiguration
implements ImportAware, SmartInitializingSingleton, BeanFactoryAware {
private static final Log logger = LogFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.annotation.Jsr250MethodSecurityMetadataSource;

@Configuration
@Configuration(proxyBeanMethods = false)
class Jsr250MetadataSourceConfiguration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @author Tadaya Tsuyukubo
* @since 5.0
*/
@Configuration
@Configuration(proxyBeanMethods = false)
class ReactiveMethodSecurityConfiguration implements ImportAware {
private int advisorOrder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public String[] selectImports(AnnotationMetadata importingClassMetadata) {
}
}

@Configuration
@Configuration(proxyBeanMethods = false)
static class OAuth2ClientWebMvcSecurityConfiguration implements WebMvcConfigurer {
private ClientRegistrationRepository clientRegistrationRepository;
private OAuth2AuthorizedClientRepository authorizedClientRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
* @author Keesun Baik
* @since 3.2
*/
@Configuration
@Configuration(proxyBeanMethods = false)
public class WebSecurityConfiguration implements ImportAware, BeanClassLoaderAware {
private WebSecurity webSecurity;

Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -52,7 +52,7 @@ public String[] selectImports(AnnotationMetadata importingClassMetadata) {
new String[] {};
}

@Configuration
@Configuration(proxyBeanMethods = false)
static class OAuth2ClientWebFluxSecurityConfiguration implements WebFluxConfigurer {
private ReactiveClientRegistrationRepository clientRegistrationRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* @author Dan Zheng
* @since 5.0
*/
@Configuration
@Configuration(proxyBeanMethods = false)
class ServerHttpSecurityConfiguration {
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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @author Rob Winch
* @since 5.0
*/
@Configuration
@Configuration(proxyBeanMethods = false)
class WebFluxSecurityConfiguration {
public static final int WEB_FILTER_CHAIN_FILTER_ORDER = 0 - 100;

Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@
import java.util.List;

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.web.method.annotation.AuthenticationPrincipalArgumentResolver;
import org.springframework.security.web.servlet.support.csrf.CsrfRequestDataValueProcessor;
Expand All @@ -37,6 +38,7 @@
* @author Rob Winch
* @since 3.2
*/
@Configuration(proxyBeanMethods = false)
@EnableWebSecurity
public class WebMvcSecurityConfiguration implements WebMvcConfigurer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ public void getAuthenticationManagerWhenAuthenticationConfigurationSubclassedThe
.isInstanceOf(AlreadyBuiltException.class);
}

@Configuration(proxyBeanMethods = false)
@Configuration
static class AuthenticationConfigurationSubclass extends AuthenticationConfiguration {
}
}
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -17,26 +17,30 @@

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
public class EnableGlobalAuthenticationTests {
@Autowired
AuthenticationConfiguration auth;
@Rule
public final SpringTestRule spring = new SpringTestRule();

// gh-4086
@Test
public void authenticationConfigurationWhenGetAuthenticationManagerThenNotNull() throws Exception {
this.spring.register(Config.class).autowire();

AuthenticationConfiguration auth = spring.getContext().getBean(AuthenticationConfiguration.class);

assertThat(auth.getAuthenticationManager()).isNotNull();
}

Expand All @@ -50,4 +54,67 @@ public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
}
}

@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() {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ public void emptyPrefixRoleUser() {}

@Test
public void methodSecurityInterceptorUsesMetadataSourceBeanWhenProxyingDisabled() {
this.spring.register(CustomMetadataSourceProxylessConfig.class).autowire();
this.spring.register(CustomMetadataSourceBeanProxyEnabledConfig.class).autowire();
MethodSecurityInterceptor methodInterceptor =
(MethodSecurityInterceptor) this.spring.getContext().getBean(MethodInterceptor.class);
MethodSecurityMetadataSource methodSecurityMetadataSource =
Expand All @@ -567,7 +567,7 @@ public void methodSecurityInterceptorUsesMetadataSourceBeanWhenProxyingDisabled(
}

@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration(proxyBeanMethods = false)
public static class CustomMetadataSourceProxylessConfig extends GlobalMethodSecurityConfiguration {
@Configuration
public static class CustomMetadataSourceBeanProxyEnabledConfig extends GlobalMethodSecurityConfiguration {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ GrantedAuthorityDefaults grantedAuthorityDefaults() {
}

@Test
public void rolePrefixWithGrantedAuthorityDefaultsAndSubclassWithProxyingDisabled() {
public void rolePrefixWithGrantedAuthorityDefaultsAndSubclassWithProxyingEnabled() {
this.spring.register(SubclassConfig.class).autowire();

TestingAuthenticationToken authentication = new TestingAuthenticationToken(
Expand All @@ -105,7 +105,7 @@ public void rolePrefixWithGrantedAuthorityDefaultsAndSubclassWithProxyingDisable
assertThat(root.hasRole("ABC")).isTrue();
}

@Configuration(proxyBeanMethods = false)
@Configuration
static class SubclassConfig extends ReactiveMethodSecurityConfiguration {
}
}
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -122,4 +122,68 @@ String principal(@AuthenticationPrincipal String principal) {
}
}
}

@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() {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public void getMethodDelegatingApplicationListenerWhenWebSecurityConfigurationTh
}

@Test
public void loadConfigWhenProxyingDisabledAndSubclassThenFilterChainsCreated() {
public void loadConfigWhenBeanProxyingEnabledAndSubclassThenFilterChainsCreated() {
this.spring.register(GlobalAuthenticationWebSecurityConfigurerAdaptersConfig.class, SubclassConfig.class).autowire();

FilterChainProxy filterChainProxy = this.spring.getContext().getBean(FilterChainProxy.class);
Expand All @@ -415,7 +415,7 @@ public void loadConfigWhenProxyingDisabledAndSubclassThenFilterChainsCreated() {
assertThat(filterChains).hasSize(4);
}

@Configuration(proxyBeanMethods = false)
@Configuration
static class SubclassConfig extends WebSecurityConfiguration {
}

Expand Down
Loading

0 comments on commit a37ce53

Please sign in to comment.