Skip to content

Remove WebSecurityConfigurerAdapter #11923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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,10 +33,10 @@
import org.springframework.ldap.core.support.BaseLdapPathContextSource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
import org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.test.SpringTestContext;
import org.springframework.security.config.test.SpringTestContextExtension;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
Expand Down Expand Up @@ -70,7 +70,6 @@ public class LdapAuthenticationProviderBuilderSecurityBuilderTests {
public void defaultConfiguration() {
this.spring.register(DefaultLdapConfig.class).autowire();
LdapAuthenticationProvider provider = ldapProvider();

LdapAuthoritiesPopulator authoritiesPopulator = getAuthoritiesPopulator(provider);
assertThat(authoritiesPopulator).hasFieldOrPropertyWithValue("groupRoleAttribute", "cn");
assertThat(authoritiesPopulator).hasFieldOrPropertyWithValue("groupSearchBase", "");
Expand Down Expand Up @@ -160,8 +159,8 @@ static int getPort() throws IOException {
@EnableWebSecurity
static class DefaultLdapConfig extends BaseLdapProviderConfig {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Autowired
void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.ldapAuthentication()
Expand All @@ -170,14 +169,20 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:on
}

@Bean
AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}

}

@Configuration
@EnableWebSecurity
static class GroupRolesConfig extends BaseLdapProviderConfig {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Autowired
void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.ldapAuthentication()
Expand All @@ -187,14 +192,20 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:on
}

@Bean
AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}

}

@Configuration
@EnableWebSecurity
static class GroupSearchConfig extends BaseLdapProviderConfig {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Autowired
void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.ldapAuthentication()
Expand All @@ -204,14 +215,20 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:on
}

@Bean
AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}

}

@Configuration
@EnableWebSecurity
static class GroupSubtreeSearchConfig extends BaseLdapProviderConfig {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Autowired
void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.ldapAuthentication()
Expand All @@ -222,14 +239,20 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:on
}

@Bean
AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}

}

@Configuration
@EnableWebSecurity
static class RolePrefixConfig extends BaseLdapProviderConfig {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Autowired
void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.ldapAuthentication()
Expand All @@ -239,14 +262,20 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:on
}

@Bean
AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}

}

@Configuration
@EnableWebSecurity
static class BindAuthenticationConfig extends BaseLdapServerConfig {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Autowired
void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.ldapAuthentication()
Expand All @@ -257,14 +286,20 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:on
}

@Bean
AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}

}

@Configuration
@EnableWebSecurity
static class PasswordEncoderConfig extends BaseLdapServerConfig {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Autowired
void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.ldapAuthentication()
Expand All @@ -276,6 +311,12 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:on
}

@Bean
AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}

}

@Configuration
Expand All @@ -296,7 +337,7 @@ ApacheDSContainer ldapServer() throws Exception {
@EnableWebSecurity
@EnableGlobalAuthentication
@Import(ObjectPostProcessorConfiguration.class)
abstract static class BaseLdapProviderConfig extends WebSecurityConfigurerAdapter {
abstract static class BaseLdapProviderConfig {

@Bean
BaseLdapPathContextSource contextSource() throws Exception {
Expand All @@ -308,15 +349,6 @@ BaseLdapPathContextSource contextSource() throws Exception {
return contextSource;
}

@Bean
AuthenticationManager authenticationManager(AuthenticationManagerBuilder auth) throws Exception {
configure(auth);
return auth.build();
}

@Override
protected abstract void configure(AuthenticationManagerBuilder auth) throws Exception;

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 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 @@ -26,7 +26,6 @@
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.ldap.LdapAuthenticationProviderBuilderSecurityBuilderTests.BaseLdapProviderConfig;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.test.SpringTestContext;
import org.springframework.security.config.test.SpringTestContextExtension;
import org.springframework.security.core.authority.AuthorityUtils;
Expand Down Expand Up @@ -105,10 +104,10 @@ public void authenticationManagerWhenSearchSubtreeThenNestedGroupFound() throws

// @formatter:off
SecurityMockMvcRequestBuilders.FormLoginRequestBuilder request = formLogin()
.user("ben")
.password("benspassword");
.user("otherben")
.password("otherbenspassword");
SecurityMockMvcResultMatchers.AuthenticatedMatcher expectedUser = authenticated()
.withUsername("ben")
.withUsername("otherben")
.withAuthorities(
AuthorityUtils.createAuthorityList("ROLE_SUBMANAGERS", "ROLE_MANAGERS", "ROLE_DEVELOPERS"));
// @formatter:on
Expand All @@ -117,10 +116,10 @@ public void authenticationManagerWhenSearchSubtreeThenNestedGroupFound() throws

@Configuration
@EnableWebSecurity
static class MultiLdapAuthenticationProvidersConfig extends WebSecurityConfigurerAdapter {
static class MultiLdapAuthenticationProvidersConfig {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Autowired
void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.ldapAuthentication()
Expand All @@ -139,10 +138,10 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {

@Configuration
@EnableWebSecurity
static class MultiLdapWithCustomRolePrefixAuthenticationProvidersConfig extends WebSecurityConfigurerAdapter {
static class MultiLdapWithCustomRolePrefixAuthenticationProvidersConfig {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Autowired
void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.ldapAuthentication()
Expand All @@ -163,18 +162,18 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {

@Configuration
@EnableWebSecurity
static class LdapWithRandomPortConfig extends WebSecurityConfigurerAdapter {
static class LdapWithRandomPortConfig {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Autowired
void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.ldapAuthentication()
.groupSearchBase("ou=groups")
.groupSearchFilter("(member={0})")
.userDnPatterns("uid={0},ou=people")
.contextSource()
.port(0);
.port(0);
// @formatter:on
}

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

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Autowired
void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.ldapAuthentication()
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-2022 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 @@ -16,10 +16,10 @@

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator;
import org.springframework.security.ldap.userdetails.PersonContextMapper;
Expand All @@ -32,10 +32,10 @@ public class NamespaceLdapAuthenticationProviderTestsConfigs {

@Configuration
@EnableWebSecurity
static class LdapAuthenticationProviderConfig extends WebSecurityConfigurerAdapter {
static class LdapAuthenticationProviderConfig {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Autowired
void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.ldapAuthentication()
Expand All @@ -48,10 +48,10 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {

@Configuration
@EnableWebSecurity
static class CustomLdapAuthenticationProviderConfig extends WebSecurityConfigurerAdapter {
static class CustomLdapAuthenticationProviderConfig {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Autowired
void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.ldapAuthentication()
Expand All @@ -70,20 +70,20 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
.managerPassword("secret") // ldap-server@manager-password
.port(0) // ldap-server@port
.root("dc=springframework,dc=org"); // ldap-server@root
// .url("ldap://localhost:33389/dc-springframework,dc=org") this overrides root and port and is used for external
// .url("ldap://localhost:33389/dc-springframework,dc=org") this overrides root and port and is used for external
// @formatter:on
}

}

@Configuration
@EnableWebSecurity
static class CustomAuthoritiesPopulatorConfig extends WebSecurityConfigurerAdapter {
static class CustomAuthoritiesPopulatorConfig {

static LdapAuthoritiesPopulator LAP;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Autowired
void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.ldapAuthentication()
Expand All @@ -96,10 +96,10 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {

@Configuration
@EnableWebSecurity
static class PasswordCompareLdapConfig extends WebSecurityConfigurerAdapter {
static class PasswordCompareLdapConfig {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Autowired
void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.ldapAuthentication()
Expand Down
Loading