Skip to content

Commit 35f7e46

Browse files
Remove WebSecurityConfigurerAdapter
Closes gh-10902
1 parent a10b0f5 commit 35f7e46

File tree

127 files changed

+2947
-4988
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+2947
-4988
lines changed

config/src/integration-test/java/org/springframework/security/config/annotation/authentication/ldap/LdapAuthenticationProviderBuilderSecurityBuilderTests.java

+59-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,10 +33,10 @@
3333
import org.springframework.ldap.core.support.BaseLdapPathContextSource;
3434
import org.springframework.security.authentication.AuthenticationManager;
3535
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
36+
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
3637
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
3738
import org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration;
3839
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
39-
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
4040
import org.springframework.security.config.test.SpringTestContext;
4141
import org.springframework.security.config.test.SpringTestContextExtension;
4242
import org.springframework.security.core.authority.SimpleGrantedAuthority;
@@ -70,7 +70,6 @@ public class LdapAuthenticationProviderBuilderSecurityBuilderTests {
7070
public void defaultConfiguration() {
7171
this.spring.register(DefaultLdapConfig.class).autowire();
7272
LdapAuthenticationProvider provider = ldapProvider();
73-
7473
LdapAuthoritiesPopulator authoritiesPopulator = getAuthoritiesPopulator(provider);
7574
assertThat(authoritiesPopulator).hasFieldOrPropertyWithValue("groupRoleAttribute", "cn");
7675
assertThat(authoritiesPopulator).hasFieldOrPropertyWithValue("groupSearchBase", "");
@@ -160,8 +159,8 @@ static int getPort() throws IOException {
160159
@EnableWebSecurity
161160
static class DefaultLdapConfig extends BaseLdapProviderConfig {
162161

163-
@Override
164-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
162+
@Autowired
163+
void configure(AuthenticationManagerBuilder auth) throws Exception {
165164
// @formatter:off
166165
auth
167166
.ldapAuthentication()
@@ -170,14 +169,20 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
170169
// @formatter:on
171170
}
172171

172+
@Bean
173+
AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
174+
throws Exception {
175+
return authenticationConfiguration.getAuthenticationManager();
176+
}
177+
173178
}
174179

175180
@Configuration
176181
@EnableWebSecurity
177182
static class GroupRolesConfig extends BaseLdapProviderConfig {
178183

179-
@Override
180-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
184+
@Autowired
185+
void configure(AuthenticationManagerBuilder auth) throws Exception {
181186
// @formatter:off
182187
auth
183188
.ldapAuthentication()
@@ -187,14 +192,20 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
187192
// @formatter:on
188193
}
189194

195+
@Bean
196+
AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
197+
throws Exception {
198+
return authenticationConfiguration.getAuthenticationManager();
199+
}
200+
190201
}
191202

192203
@Configuration
193204
@EnableWebSecurity
194205
static class GroupSearchConfig extends BaseLdapProviderConfig {
195206

196-
@Override
197-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
207+
@Autowired
208+
void configure(AuthenticationManagerBuilder auth) throws Exception {
198209
// @formatter:off
199210
auth
200211
.ldapAuthentication()
@@ -204,14 +215,20 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
204215
// @formatter:on
205216
}
206217

218+
@Bean
219+
AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
220+
throws Exception {
221+
return authenticationConfiguration.getAuthenticationManager();
222+
}
223+
207224
}
208225

209226
@Configuration
210227
@EnableWebSecurity
211228
static class GroupSubtreeSearchConfig extends BaseLdapProviderConfig {
212229

213-
@Override
214-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
230+
@Autowired
231+
void configure(AuthenticationManagerBuilder auth) throws Exception {
215232
// @formatter:off
216233
auth
217234
.ldapAuthentication()
@@ -222,14 +239,20 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
222239
// @formatter:on
223240
}
224241

242+
@Bean
243+
AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
244+
throws Exception {
245+
return authenticationConfiguration.getAuthenticationManager();
246+
}
247+
225248
}
226249

227250
@Configuration
228251
@EnableWebSecurity
229252
static class RolePrefixConfig extends BaseLdapProviderConfig {
230253

231-
@Override
232-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
254+
@Autowired
255+
void configure(AuthenticationManagerBuilder auth) throws Exception {
233256
// @formatter:off
234257
auth
235258
.ldapAuthentication()
@@ -239,14 +262,20 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
239262
// @formatter:on
240263
}
241264

265+
@Bean
266+
AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
267+
throws Exception {
268+
return authenticationConfiguration.getAuthenticationManager();
269+
}
270+
242271
}
243272

244273
@Configuration
245274
@EnableWebSecurity
246275
static class BindAuthenticationConfig extends BaseLdapServerConfig {
247276

248-
@Override
249-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
277+
@Autowired
278+
void configure(AuthenticationManagerBuilder auth) throws Exception {
250279
// @formatter:off
251280
auth
252281
.ldapAuthentication()
@@ -257,14 +286,20 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
257286
// @formatter:on
258287
}
259288

289+
@Bean
290+
AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
291+
throws Exception {
292+
return authenticationConfiguration.getAuthenticationManager();
293+
}
294+
260295
}
261296

262297
@Configuration
263298
@EnableWebSecurity
264299
static class PasswordEncoderConfig extends BaseLdapServerConfig {
265300

266-
@Override
267-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
301+
@Autowired
302+
void configure(AuthenticationManagerBuilder auth) throws Exception {
268303
// @formatter:off
269304
auth
270305
.ldapAuthentication()
@@ -276,6 +311,12 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
276311
// @formatter:on
277312
}
278313

314+
@Bean
315+
AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
316+
throws Exception {
317+
return authenticationConfiguration.getAuthenticationManager();
318+
}
319+
279320
}
280321

281322
@Configuration
@@ -296,7 +337,7 @@ ApacheDSContainer ldapServer() throws Exception {
296337
@EnableWebSecurity
297338
@EnableGlobalAuthentication
298339
@Import(ObjectPostProcessorConfiguration.class)
299-
abstract static class BaseLdapProviderConfig extends WebSecurityConfigurerAdapter {
340+
abstract static class BaseLdapProviderConfig {
300341

301342
@Bean
302343
BaseLdapPathContextSource contextSource() throws Exception {
@@ -308,15 +349,6 @@ BaseLdapPathContextSource contextSource() throws Exception {
308349
return contextSource;
309350
}
310351

311-
@Bean
312-
AuthenticationManager authenticationManager(AuthenticationManagerBuilder auth) throws Exception {
313-
configure(auth);
314-
return auth.build();
315-
}
316-
317-
@Override
318-
protected abstract void configure(AuthenticationManagerBuilder auth) throws Exception;
319-
320352
}
321353

322354
}

config/src/integration-test/java/org/springframework/security/config/annotation/authentication/ldap/LdapAuthenticationProviderConfigurerTests.java

+16-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@
2626
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
2727
import org.springframework.security.config.annotation.authentication.ldap.LdapAuthenticationProviderBuilderSecurityBuilderTests.BaseLdapProviderConfig;
2828
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
29-
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
3029
import org.springframework.security.config.test.SpringTestContext;
3130
import org.springframework.security.config.test.SpringTestContextExtension;
3231
import org.springframework.security.core.authority.AuthorityUtils;
@@ -105,10 +104,10 @@ public void authenticationManagerWhenSearchSubtreeThenNestedGroupFound() throws
105104

106105
// @formatter:off
107106
SecurityMockMvcRequestBuilders.FormLoginRequestBuilder request = formLogin()
108-
.user("ben")
109-
.password("benspassword");
107+
.user("otherben")
108+
.password("otherbenspassword");
110109
SecurityMockMvcResultMatchers.AuthenticatedMatcher expectedUser = authenticated()
111-
.withUsername("ben")
110+
.withUsername("otherben")
112111
.withAuthorities(
113112
AuthorityUtils.createAuthorityList("ROLE_SUBMANAGERS", "ROLE_MANAGERS", "ROLE_DEVELOPERS"));
114113
// @formatter:on
@@ -117,10 +116,10 @@ public void authenticationManagerWhenSearchSubtreeThenNestedGroupFound() throws
117116

118117
@Configuration
119118
@EnableWebSecurity
120-
static class MultiLdapAuthenticationProvidersConfig extends WebSecurityConfigurerAdapter {
119+
static class MultiLdapAuthenticationProvidersConfig {
121120

122-
@Override
123-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
121+
@Autowired
122+
void configure(AuthenticationManagerBuilder auth) throws Exception {
124123
// @formatter:off
125124
auth
126125
.ldapAuthentication()
@@ -139,10 +138,10 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
139138

140139
@Configuration
141140
@EnableWebSecurity
142-
static class MultiLdapWithCustomRolePrefixAuthenticationProvidersConfig extends WebSecurityConfigurerAdapter {
141+
static class MultiLdapWithCustomRolePrefixAuthenticationProvidersConfig {
143142

144-
@Override
145-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
143+
@Autowired
144+
void configure(AuthenticationManagerBuilder auth) throws Exception {
146145
// @formatter:off
147146
auth
148147
.ldapAuthentication()
@@ -163,18 +162,18 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
163162

164163
@Configuration
165164
@EnableWebSecurity
166-
static class LdapWithRandomPortConfig extends WebSecurityConfigurerAdapter {
165+
static class LdapWithRandomPortConfig {
167166

168-
@Override
169-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
167+
@Autowired
168+
void configure(AuthenticationManagerBuilder auth) throws Exception {
170169
// @formatter:off
171170
auth
172171
.ldapAuthentication()
173172
.groupSearchBase("ou=groups")
174173
.groupSearchFilter("(member={0})")
175174
.userDnPatterns("uid={0},ou=people")
176175
.contextSource()
177-
.port(0);
176+
.port(0);
178177
// @formatter:on
179178
}
180179

@@ -184,8 +183,8 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
184183
@EnableWebSecurity
185184
static class GroupSubtreeSearchConfig extends BaseLdapProviderConfig {
186185

187-
@Override
188-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
186+
@Autowired
187+
void configure(AuthenticationManagerBuilder auth) throws Exception {
189188
// @formatter:off
190189
auth
191190
.ldapAuthentication()

config/src/integration-test/java/org/springframework/security/config/annotation/authentication/ldap/NamespaceLdapAuthenticationProviderTestsConfigs.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,10 @@
1616

1717
package org.springframework.security.config.annotation.authentication.ldap;
1818

19+
import org.springframework.beans.factory.annotation.Autowired;
1920
import org.springframework.context.annotation.Configuration;
2021
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
2122
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
22-
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
2323
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
2424
import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator;
2525
import org.springframework.security.ldap.userdetails.PersonContextMapper;
@@ -32,10 +32,10 @@ public class NamespaceLdapAuthenticationProviderTestsConfigs {
3232

3333
@Configuration
3434
@EnableWebSecurity
35-
static class LdapAuthenticationProviderConfig extends WebSecurityConfigurerAdapter {
35+
static class LdapAuthenticationProviderConfig {
3636

37-
@Override
38-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
37+
@Autowired
38+
void configure(AuthenticationManagerBuilder auth) throws Exception {
3939
// @formatter:off
4040
auth
4141
.ldapAuthentication()
@@ -48,10 +48,10 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
4848

4949
@Configuration
5050
@EnableWebSecurity
51-
static class CustomLdapAuthenticationProviderConfig extends WebSecurityConfigurerAdapter {
51+
static class CustomLdapAuthenticationProviderConfig {
5252

53-
@Override
54-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
53+
@Autowired
54+
void configure(AuthenticationManagerBuilder auth) throws Exception {
5555
// @formatter:off
5656
auth
5757
.ldapAuthentication()
@@ -70,20 +70,20 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
7070
.managerPassword("secret") // ldap-server@manager-password
7171
.port(0) // ldap-server@port
7272
.root("dc=springframework,dc=org"); // ldap-server@root
73-
// .url("ldap://localhost:33389/dc-springframework,dc=org") this overrides root and port and is used for external
73+
// .url("ldap://localhost:33389/dc-springframework,dc=org") this overrides root and port and is used for external
7474
// @formatter:on
7575
}
7676

7777
}
7878

7979
@Configuration
8080
@EnableWebSecurity
81-
static class CustomAuthoritiesPopulatorConfig extends WebSecurityConfigurerAdapter {
81+
static class CustomAuthoritiesPopulatorConfig {
8282

8383
static LdapAuthoritiesPopulator LAP;
8484

85-
@Override
86-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
85+
@Autowired
86+
void configure(AuthenticationManagerBuilder auth) throws Exception {
8787
// @formatter:off
8888
auth
8989
.ldapAuthentication()
@@ -96,10 +96,10 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
9696

9797
@Configuration
9898
@EnableWebSecurity
99-
static class PasswordCompareLdapConfig extends WebSecurityConfigurerAdapter {
99+
static class PasswordCompareLdapConfig {
100100

101-
@Override
102-
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
101+
@Autowired
102+
void configure(AuthenticationManagerBuilder auth) throws Exception {
103103
// @formatter:off
104104
auth
105105
.ldapAuthentication()

0 commit comments

Comments
 (0)