Fix Add @Configuration to @Enable*Security Usage

Issue gh-6613
This commit is contained in:
Rob Winch 2022-08-09 07:37:59 -05:00
parent 040111ae9e
commit a5069d7e35
16 changed files with 57 additions and 8 deletions

View File

@ -189,6 +189,7 @@ public class LdapAuthenticationProviderBuilderSecurityBuilderTests {
} }
@Configuration
@EnableWebSecurity @EnableWebSecurity
static class GroupSearchConfig extends BaseLdapProviderConfig { static class GroupSearchConfig extends BaseLdapProviderConfig {

View File

@ -55,6 +55,7 @@ import org.springframework.security.rsocket.util.matcher.RoutePayloadExchangeMat
* A minimal example can be found below: * A minimal example can be found below:
* *
* <pre class="code"> * <pre class="code">
* &#064;Configuration
* &#064;EnableRSocketSecurity * &#064;EnableRSocketSecurity
* public class SecurityConfig { * public class SecurityConfig {
* &#064;Bean * &#064;Bean
@ -82,6 +83,7 @@ import org.springframework.security.rsocket.util.matcher.RoutePayloadExchangeMat
* A more advanced configuration can be seen below: * A more advanced configuration can be seen below:
* *
* <pre class="code"> * <pre class="code">
* &#064;Configuration
* &#064;EnableRSocketSecurity * &#064;EnableRSocketSecurity
* public class SecurityConfig { * public class SecurityConfig {
* &#064;Bean * &#064;Bean

View File

@ -22,7 +22,6 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication; import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
@ -39,7 +38,6 @@ import org.springframework.security.config.annotation.authentication.configurati
@Documented @Documented
@Import(WebMvcSecurityConfiguration.class) @Import(WebMvcSecurityConfiguration.class)
@EnableGlobalAuthentication @EnableGlobalAuthentication
@Configuration
@Deprecated @Deprecated
public @interface EnableWebMvcSecurity { public @interface EnableWebMvcSecurity {

View File

@ -39,9 +39,9 @@ import org.springframework.util.Assert;
* @author Rob Winch * @author Rob Winch
* *
*/ */
@Configuration
@EnableWebSecurity @EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) @EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter { public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired @Autowired

View File

@ -531,8 +531,8 @@ public class GlobalMethodSecurityConfigurationTests {
} }
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration @Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public static class RoleHierarchyConfig { public static class RoleHierarchyConfig {
@Bean @Bean
@ -607,8 +607,8 @@ public class GlobalMethodSecurityConfigurationTests {
} }
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration @Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public static class CustomMetadataSourceBeanProxyEnabledConfig extends GlobalMethodSecurityConfiguration { public static class CustomMetadataSourceBeanProxyEnabledConfig extends GlobalMethodSecurityConfiguration {
} }

View File

@ -316,8 +316,8 @@ public class NamespaceGlobalMethodSecurityTests {
} }
@EnableGlobalMethodSecurity(jsr250Enabled = true)
@Configuration @Configuration
@EnableGlobalMethodSecurity(jsr250Enabled = true)
public static class Jsr250Config { public static class Jsr250Config {
} }

View File

@ -127,6 +127,7 @@ public class AnonymousConfigurerTests {
} }
@Configuration
@EnableWebSecurity @EnableWebSecurity
static class AnonymousDisabledInLambdaConfig extends WebSecurityConfigurerAdapter { static class AnonymousDisabledInLambdaConfig extends WebSecurityConfigurerAdapter {

View File

@ -131,6 +131,7 @@ public class NamespaceHttpFormLoginTests {
} }
@Configuration
@EnableWebSecurity @EnableWebSecurity
static class FormLoginCustomConfig extends WebSecurityConfigurerAdapter { static class FormLoginCustomConfig extends WebSecurityConfigurerAdapter {

View File

@ -26,6 +26,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
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.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.security.config.annotation.ObjectPostProcessor; import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig; import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig;
@ -151,6 +152,7 @@ public class X509ConfigurerTests {
} }
} }
@Configuration
@EnableWebSecurity @EnableWebSecurity
static class ObjectPostProcessorConfig extends WebSecurityConfigurerAdapter { static class ObjectPostProcessorConfig extends WebSecurityConfigurerAdapter {
@ -180,6 +182,7 @@ public class X509ConfigurerTests {
} }
@Configuration
@EnableWebSecurity @EnableWebSecurity
static class DuplicateDoesNotOverrideConfig extends WebSecurityConfigurerAdapter { static class DuplicateDoesNotOverrideConfig extends WebSecurityConfigurerAdapter {
@ -205,6 +208,7 @@ public class X509ConfigurerTests {
} }
@Configuration
@EnableWebSecurity @EnableWebSecurity
static class DefaultsInLambdaConfig extends WebSecurityConfigurerAdapter { static class DefaultsInLambdaConfig extends WebSecurityConfigurerAdapter {
@ -227,6 +231,7 @@ public class X509ConfigurerTests {
} }
@Configuration
@EnableWebSecurity @EnableWebSecurity
static class SubjectPrincipalRegexInLambdaConfig extends WebSecurityConfigurerAdapter { static class SubjectPrincipalRegexInLambdaConfig extends WebSecurityConfigurerAdapter {
@ -252,6 +257,7 @@ public class X509ConfigurerTests {
} }
@Configuration
@EnableWebSecurity @EnableWebSecurity
static class UserDetailsServiceBeanConfig { static class UserDetailsServiceBeanConfig {
@ -279,6 +285,7 @@ public class X509ConfigurerTests {
} }
@Configuration
@EnableWebSecurity @EnableWebSecurity
static class UserDetailsServiceAndBeanConfig { static class UserDetailsServiceAndBeanConfig {

View File

@ -210,8 +210,8 @@ class KotlinEnableReactiveMethodSecurityTests {
verify { delegate wasNot Called } verify { delegate wasNot Called }
} }
@EnableReactiveMethodSecurity
@Configuration @Configuration
@EnableReactiveMethodSecurity
open class Config { open class Config {
var delegate = mockk<KotlinReactiveMessageService>() var delegate = mockk<KotlinReactiveMessageService>()

View File

@ -32,7 +32,7 @@ import org.springframework.test.web.reactive.server.WebTestClient
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
import org.springframework.web.reactive.config.EnableWebFlux import org.springframework.web.reactive.config.EnableWebFlux
import java.util.* import java.util.Base64
/** /**
* Tests for [AuthorizeExchangeDsl] * Tests for [AuthorizeExchangeDsl]

View File

@ -1,3 +1,4 @@
[[jc-method]] [[jc-method]]
= Method Security = Method Security
@ -32,6 +33,7 @@ For example, the following would enable Spring Security's `@PreAuthorize` annota
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableMethodSecurity @EnableMethodSecurity
public class MethodSecurityConfig { public class MethodSecurityConfig {
// ... // ...
@ -41,6 +43,7 @@ public class MethodSecurityConfig {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableMethodSecurity @EnableMethodSecurity
class MethodSecurityConfig { class MethodSecurityConfig {
// ... // ...
@ -98,6 +101,7 @@ You can enable support for Spring Security's `@Secured` annotation using:
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableMethodSecurity(securedEnabled = true) @EnableMethodSecurity(securedEnabled = true)
public class MethodSecurityConfig { public class MethodSecurityConfig {
// ... // ...
@ -107,6 +111,7 @@ public class MethodSecurityConfig {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableMethodSecurity(securedEnabled = true) @EnableMethodSecurity(securedEnabled = true)
class MethodSecurityConfig { class MethodSecurityConfig {
// ... // ...
@ -127,6 +132,7 @@ or JSR-250 using:
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableMethodSecurity(jsr250Enabled = true) @EnableMethodSecurity(jsr250Enabled = true)
public class MethodSecurityConfig { public class MethodSecurityConfig {
// ... // ...
@ -136,6 +142,7 @@ public class MethodSecurityConfig {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableMethodSecurity(jsr250Enabled = true) @EnableMethodSecurity(jsr250Enabled = true)
class MethodSecurityConfig { class MethodSecurityConfig {
// ... // ...
@ -264,6 +271,7 @@ To recreate what adding `@EnableMethodSecurity` does by default, you would publi
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableMethodSecurity(prePostEnabled = false) @EnableMethodSecurity(prePostEnabled = false)
class MethodSecurityConfig { class MethodSecurityConfig {
@Bean @Bean
@ -295,6 +303,7 @@ class MethodSecurityConfig {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableMethodSecurity(prePostEnabled = false) @EnableMethodSecurity(prePostEnabled = false)
class MethodSecurityConfig { class MethodSecurityConfig {
@Bean @Bean
@ -392,6 +401,7 @@ You may want to only support `@PreAuthorize` in your application, in which case
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableMethodSecurity(prePostEnabled = false) @EnableMethodSecurity(prePostEnabled = false)
class MethodSecurityConfig { class MethodSecurityConfig {
@Bean @Bean
@ -405,6 +415,7 @@ class MethodSecurityConfig {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableMethodSecurity(prePostEnabled = false) @EnableMethodSecurity(prePostEnabled = false)
class MethodSecurityConfig { class MethodSecurityConfig {
@Bean @Bean
@ -440,6 +451,7 @@ Thus, you can configure Spring Security to invoke your `AuthorizationManager` in
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableMethodSecurity @EnableMethodSecurity
class MethodSecurityConfig { class MethodSecurityConfig {
@Bean @Bean
@ -458,6 +470,7 @@ class MethodSecurityConfig {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableMethodSecurity @EnableMethodSecurity
class MethodSecurityConfig { class MethodSecurityConfig {
@Bean @Bean
@ -542,6 +555,7 @@ For example, if you have your own custom annotation, you can configure it like s
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableMethodSecurity @EnableMethodSecurity
class MethodSecurityConfig { class MethodSecurityConfig {
@Bean @Bean
@ -558,6 +572,7 @@ class MethodSecurityConfig {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableMethodSecurity @EnableMethodSecurity
class MethodSecurityConfig { class MethodSecurityConfig {
@Bean @Bean
@ -607,6 +622,7 @@ The following example enables Spring Security's `@Secured` annotation:
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true) @EnableGlobalMethodSecurity(securedEnabled = true)
public class MethodSecurityConfig { public class MethodSecurityConfig {
// ... // ...
@ -616,6 +632,7 @@ public class MethodSecurityConfig {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true) @EnableGlobalMethodSecurity(securedEnabled = true)
open class MethodSecurityConfig { open class MethodSecurityConfig {
// ... // ...
@ -666,6 +683,7 @@ Support for JSR-250 annotations can be enabled by using:
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableGlobalMethodSecurity(jsr250Enabled = true) @EnableGlobalMethodSecurity(jsr250Enabled = true)
public class MethodSecurityConfig { public class MethodSecurityConfig {
// ... // ...
@ -675,6 +693,7 @@ public class MethodSecurityConfig {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableGlobalMethodSecurity(jsr250Enabled = true) @EnableGlobalMethodSecurity(jsr250Enabled = true)
open class MethodSecurityConfig { open class MethodSecurityConfig {
// ... // ...
@ -689,6 +708,7 @@ To use the new expression-based syntax, you would use:
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true) @EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig { public class MethodSecurityConfig {
// ... // ...
@ -698,6 +718,7 @@ public class MethodSecurityConfig {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true) @EnableGlobalMethodSecurity(prePostEnabled = true)
open class MethodSecurityConfig { open class MethodSecurityConfig {
// ... // ...
@ -750,6 +771,7 @@ For example, if you wanted to provide a custom `MethodSecurityExpressionHandler`
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true) @EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration { public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Override @Override
@ -763,6 +785,7 @@ public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true) @EnableGlobalMethodSecurity(prePostEnabled = true)
open class MethodSecurityConfig : GlobalMethodSecurityConfiguration() { open class MethodSecurityConfig : GlobalMethodSecurityConfiguration() {
override fun createExpressionHandler(): MethodSecurityExpressionHandler { override fun createExpressionHandler(): MethodSecurityExpressionHandler {

View File

@ -324,6 +324,7 @@ You can also explicit disable the default:
==== ====
[source,java] [source,java]
---- ----
@Configuration
@EnableWebSecurity @EnableWebSecurity
public class Config { public class Config {
@Bean @Bean

View File

@ -887,6 +887,7 @@ You can enable the preceding permissions policy header using the following confi
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableWebSecurity @EnableWebSecurity
public class WebSecurityConfig { public class WebSecurityConfig {
@ -919,6 +920,7 @@ public class WebSecurityConfig {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableWebSecurity @EnableWebSecurity
class SecurityConfig { class SecurityConfig {
@ -958,6 +960,7 @@ You can send the preceding header on log out with the following configuration:
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableWebSecurity @EnableWebSecurity
public class WebSecurityConfig { public class WebSecurityConfig {
@ -976,6 +979,7 @@ public class WebSecurityConfig {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableWebSecurity @EnableWebSecurity
class SecurityConfig { class SecurityConfig {
@ -1015,6 +1019,7 @@ Given the preceding header, you could add the headers to the response by using t
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableWebSecurity @EnableWebSecurity
public class WebSecurityConfig { public class WebSecurityConfig {
@ -1045,6 +1050,7 @@ public class WebSecurityConfig {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableWebSecurity @EnableWebSecurity
class SecurityConfig { class SecurityConfig {
@ -1074,6 +1080,7 @@ If you wanted to explicitly configure <<servlet-headers-frame-options>>, you cou
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableWebSecurity @EnableWebSecurity
public class WebSecurityConfig { public class WebSecurityConfig {
@ -1110,6 +1117,7 @@ See https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsi
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableWebSecurity @EnableWebSecurity
class SecurityConfig { class SecurityConfig {
@ -1141,6 +1149,7 @@ The following configuration example uses `DelegatingRequestMatcherHeaderWriter`:
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableWebSecurity @EnableWebSecurity
public class WebSecurityConfig { public class WebSecurityConfig {
@ -1188,6 +1197,7 @@ public class WebSecurityConfig {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableWebSecurity @EnableWebSecurity
class SecurityConfig { class SecurityConfig {

View File

@ -931,6 +931,7 @@ Also, you can configure `OidcClientInitiatedLogoutSuccessHandler`, which impleme
.Java .Java
[source,java,role="primary"] [source,java,role="primary"]
---- ----
@Configuration
@EnableWebSecurity @EnableWebSecurity
public class OAuth2LoginSecurityConfig { public class OAuth2LoginSecurityConfig {
@ -966,6 +967,7 @@ public class OAuth2LoginSecurityConfig {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableWebSecurity @EnableWebSecurity
class OAuth2LoginSecurityConfig { class OAuth2LoginSecurityConfig {
@Autowired @Autowired

View File

@ -757,6 +757,7 @@ public class DirectlyConfiguredJwkSetUri {
.Kotlin .Kotlin
[source,kotlin,role="secondary"] [source,kotlin,role="secondary"]
---- ----
@Configuration
@EnableWebSecurity @EnableWebSecurity
class DirectlyConfiguredJwkSetUri { class DirectlyConfiguredJwkSetUri {
@Bean @Bean
@ -949,6 +950,7 @@ static class CustomAuthenticationConverter implements Converter<Jwt, AbstractAut
// ... // ...
@Configuration
@EnableWebSecurity @EnableWebSecurity
public class CustomAuthenticationConverterConfig { public class CustomAuthenticationConverterConfig {
@Bean @Bean
@ -978,6 +980,7 @@ internal class CustomAuthenticationConverter : Converter<Jwt, AbstractAuthentica
// ... // ...
@Configuration
@EnableWebSecurity @EnableWebSecurity
class CustomAuthenticationConverterConfig { class CustomAuthenticationConverterConfig {
@Bean @Bean