Skip to content

Commit 6c630bf

Browse files
committed
2 parents 797f42a + a4f813a commit 6c630bf

File tree

109 files changed

+1205
-367
lines changed

Some content is hidden

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

109 files changed

+1205
-367
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.IOException;
2020
import java.net.ServerSocket;
21-
import java.util.Collections;
2221
import java.util.List;
2322

2423
import javax.naming.directory.SearchControls;
@@ -39,7 +38,6 @@
3938
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
4039
import org.springframework.security.config.test.SpringTestContext;
4140
import org.springframework.security.config.test.SpringTestContextExtension;
42-
import org.springframework.security.core.authority.SimpleGrantedAuthority;
4341
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
4442
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
4543
import org.springframework.security.ldap.DefaultSpringSecurityContextSource;
@@ -120,8 +118,7 @@ public void bindAuthentication() throws Exception {
120118
this.spring.register(BindAuthenticationConfig.class).autowire();
121119

122120
this.mockMvc.perform(formLogin().user("bob").password("bobspassword"))
123-
.andExpect(authenticated().withUsername("bob")
124-
.withAuthorities(Collections.singleton(new SimpleGrantedAuthority("ROLE_DEVELOPERS"))));
121+
.andExpect(authenticated().withUsername("bob").withRoles("DEVELOPERS"));
125122
}
126123

127124
// SEC-2472
@@ -130,8 +127,7 @@ public void canUseCryptoPasswordEncoder() throws Exception {
130127
this.spring.register(PasswordEncoderConfig.class).autowire();
131128

132129
this.mockMvc.perform(formLogin().user("bcrypt").password("password"))
133-
.andExpect(authenticated().withUsername("bcrypt")
134-
.withAuthorities(Collections.singleton(new SimpleGrantedAuthority("ROLE_DEVELOPERS"))));
130+
.andExpect(authenticated().withUsername("bcrypt").withRoles("DEVELOPERS"));
135131
}
136132

137133
private LdapAuthenticationProvider ldapProvider() {

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

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

19-
import java.util.Collections;
20-
2119
import org.junit.jupiter.api.Test;
2220
import org.junit.jupiter.api.extension.ExtendWith;
2321

@@ -28,8 +26,6 @@
2826
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
2927
import org.springframework.security.config.test.SpringTestContext;
3028
import org.springframework.security.config.test.SpringTestContextExtension;
31-
import org.springframework.security.core.authority.AuthorityUtils;
32-
import org.springframework.security.core.authority.SimpleGrantedAuthority;
3329
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders;
3430
import org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers;
3531
import org.springframework.test.web.servlet.MockMvc;
@@ -64,7 +60,7 @@ public void authenticationManagerSupportMultipleLdapContextWithDefaultRolePrefix
6460
.password("bobspassword");
6561
SecurityMockMvcResultMatchers.AuthenticatedMatcher expectedUser = authenticated()
6662
.withUsername("bob")
67-
.withAuthorities(Collections.singleton(new SimpleGrantedAuthority("ROLE_DEVELOPERS")));
63+
.withRoles("DEVELOPERS");
6864
// @formatter:on
6965
this.mockMvc.perform(request).andExpect(expectedUser);
7066
}
@@ -79,7 +75,7 @@ public void authenticationManagerSupportMultipleLdapContextWithCustomRolePrefix(
7975
.password("bobspassword");
8076
SecurityMockMvcResultMatchers.AuthenticatedMatcher expectedUser = authenticated()
8177
.withUsername("bob")
82-
.withAuthorities(Collections.singleton(new SimpleGrantedAuthority("ROL_DEVELOPERS")));
78+
.withRoles("ROL_", new String[] { "DEVELOPERS" });
8379
// @formatter:on
8480
this.mockMvc.perform(request).andExpect(expectedUser);
8581
}
@@ -108,8 +104,7 @@ public void authenticationManagerWhenSearchSubtreeThenNestedGroupFound() throws
108104
.password("otherbenspassword");
109105
SecurityMockMvcResultMatchers.AuthenticatedMatcher expectedUser = authenticated()
110106
.withUsername("otherben")
111-
.withAuthorities(
112-
AuthorityUtils.createAuthorityList("ROLE_SUBMANAGERS", "ROLE_MANAGERS", "ROLE_DEVELOPERS"));
107+
.withRoles("SUBMANAGERS", "MANAGERS", "DEVELOPERS");
113108
// @formatter:on
114109
this.mockMvc.perform(request).andExpect(expectedUser);
115110
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

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

19-
import java.util.Collections;
2019
import java.util.HashSet;
2120
import java.util.Set;
2221

@@ -34,7 +33,6 @@
3433
import org.springframework.security.config.test.SpringTestContextExtension;
3534
import org.springframework.security.core.GrantedAuthority;
3635
import org.springframework.security.core.authority.AuthorityUtils;
37-
import org.springframework.security.core.authority.SimpleGrantedAuthority;
3836
import org.springframework.security.ldap.DefaultSpringSecurityContextSource;
3937
import org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator;
4038
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders;
@@ -79,7 +77,7 @@ public void ldapAuthenticationProviderCustom() throws Exception {
7977
.user("bob")
8078
.password("bobspassword");
8179
SecurityMockMvcResultMatchers.AuthenticatedMatcher user = authenticated()
82-
.withAuthorities(Collections.singleton(new SimpleGrantedAuthority("PREFIX_DEVELOPERS")));
80+
.withRoles("PREFIX_", new String[] { "DEVELOPERS" });
8381
// @formatter:on
8482
this.mockMvc.perform(request).andExpect(user);
8583
}
@@ -103,7 +101,7 @@ protected Set<GrantedAuthority> getAdditionalRoles(DirContextOperations user, St
103101
.user("bob")
104102
.password("bobspassword");
105103
SecurityMockMvcResultMatchers.AuthenticatedMatcher user = authenticated()
106-
.withAuthorities(Collections.singleton(new SimpleGrantedAuthority("ROLE_EXTRA")));
104+
.withRoles("EXTRA");
107105
// @formatter:on
108106
this.mockMvc.perform(request).andExpect(user);
109107
}

config/src/integration-test/java/org/springframework/security/config/ldap/LdapProviderBeanDefinitionParserTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public void ldapAuthenticationProviderWorksWithPlaceholders() {
169169
this.appCtx = new InMemoryXmlApplicationContext("<ldap-server />" + "<authentication-manager>"
170170
+ " <ldap-authentication-provider user-dn-pattern='uid={0},ou=${udp}' group-search-filter='${gsf}={0}' />"
171171
+ "</authentication-manager>"
172-
+ "<b:bean id='org.springframework.beans.factory.config.PropertyPlaceholderConfigurer' class='org.springframework.beans.factory.config.PropertyPlaceholderConfigurer' />");
172+
+ "<b:bean id='org.springframework.context.support.PropertySourcesPlaceholderConfigurer' class='org.springframework.context.support.PropertySourcesPlaceholderConfigurer' />");
173173

174174
ProviderManager providerManager = this.appCtx.getBean(BeanIds.AUTHENTICATION_MANAGER, ProviderManager.class);
175175
assertThat(providerManager.getProviders()).hasSize(1);

config/src/main/java/org/springframework/security/config/web/server/ServerHttpSecurity.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3033,7 +3033,8 @@ private LogoutSpec() {
30333033

30343034
/**
30353035
* Configures the logout handler. Default is
3036-
* {@code SecurityContextServerLogoutHandler}
3036+
* {@code SecurityContextServerLogoutHandler}. This clears any previous handlers
3037+
* configured.
30373038
* @param logoutHandler
30383039
* @return the {@link LogoutSpec} to configure
30393040
*/
@@ -3049,6 +3050,18 @@ private LogoutSpec addLogoutHandler(ServerLogoutHandler logoutHandler) {
30493050
return this;
30503051
}
30513052

3053+
/**
3054+
* Allows managing the list of {@link ServerLogoutHandler} instances.
3055+
* @param handlersConsumer {@link Consumer} for managing the list of handlers.
3056+
* @return the {@link LogoutSpec} to configure
3057+
* @since 7.0
3058+
*/
3059+
public LogoutSpec logoutHandler(Consumer<List<ServerLogoutHandler>> handlersConsumer) {
3060+
Assert.notNull(handlersConsumer, "consumer cannot be null");
3061+
handlersConsumer.accept(this.logoutHandlers);
3062+
return this;
3063+
}
3064+
30523065
/**
30533066
* Configures what URL a POST to will trigger a log out.
30543067
* @param logoutUrl the url to trigger a log out (i.e. "/signout" would mean a

config/src/test/java/org/springframework/security/config/annotation/authentication/AuthenticationManagerBuilderTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.security.authentication.AuthenticationManager;
3333
import org.springframework.security.authentication.AuthenticationProvider;
3434
import org.springframework.security.authentication.ProviderManager;
35+
import org.springframework.security.authentication.SecurityAssertions;
3536
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
3637
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
3738
import org.springframework.security.config.ObjectPostProcessor;
@@ -44,7 +45,6 @@
4445
import org.springframework.security.config.test.SpringTestContextExtension;
4546
import org.springframework.security.core.Authentication;
4647
import org.springframework.security.core.AuthenticationException;
47-
import org.springframework.security.core.GrantedAuthority;
4848
import org.springframework.security.core.userdetails.PasswordEncodedUser;
4949
import org.springframework.security.core.userdetails.UserDetailsService;
5050
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
@@ -107,8 +107,7 @@ public void getAuthenticationManagerWhenGlobalPasswordEncoderBeanThenUsed() thro
107107
.getAuthenticationManager();
108108
Authentication auth = manager
109109
.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("user", "password"));
110-
assertThat(auth.getName()).isEqualTo("user");
111-
assertThat(auth.getAuthorities()).extracting(GrantedAuthority::getAuthority).containsOnly("ROLE_USER");
110+
SecurityAssertions.assertThat(auth).name("user").hasAuthority("ROLE_USER");
112111
}
113112

114113
@Test
@@ -119,8 +118,7 @@ public void getAuthenticationManagerWhenProtectedPasswordEncoderBeanThenUsed() t
119118
.getAuthenticationManager();
120119
Authentication auth = manager
121120
.authenticate(UsernamePasswordAuthenticationToken.unauthenticated("user", "password"));
122-
assertThat(auth.getName()).isEqualTo("user");
123-
assertThat(auth.getAuthorities()).extracting(GrantedAuthority::getAuthority).containsOnly("ROLE_USER");
121+
SecurityAssertions.assertThat(auth).name("user").hasAuthority("ROLE_USER");
124122
}
125123

126124
@Test

config/src/test/java/org/springframework/security/config/annotation/web/configurers/WebAuthnConfigurerTests.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,15 @@ UserDetailsService userDetailsService() {
300300

301301
@Bean
302302
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
303-
return http.formLogin(Customizer.withDefaults()).webAuthn(Customizer.withDefaults()).build();
303+
// @formatter:off
304+
http
305+
.formLogin(Customizer.withDefaults())
306+
.webAuthn((authn) -> authn
307+
.rpId("spring.io")
308+
.rpName("spring")
309+
);
310+
// @formatter:on
311+
return http.build();
304312
}
305313

306314
}
@@ -316,7 +324,14 @@ UserDetailsService userDetailsService() {
316324

317325
@Bean
318326
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
319-
return http.webAuthn(Customizer.withDefaults()).build();
327+
// @formatter:off
328+
http
329+
.webAuthn((authn) -> authn
330+
.rpId("spring.io")
331+
.rpName("spring")
332+
);
333+
// @formatter:on
334+
return http.build();
320335
}
321336

322337
}
@@ -332,9 +347,16 @@ UserDetailsService userDetailsService() {
332347

333348
@Bean
334349
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
335-
return http.formLogin(Customizer.withDefaults())
336-
.webAuthn((webauthn) -> webauthn.disableDefaultRegistrationPage(true))
337-
.build();
350+
// @formatter:off
351+
http
352+
.formLogin(Customizer.withDefaults())
353+
.webAuthn((authn) -> authn
354+
.rpId("spring.io")
355+
.rpName("spring")
356+
.disableDefaultRegistrationPage(true)
357+
);
358+
// @formatter:on
359+
return http.build();
338360
}
339361

340362
}
@@ -350,9 +372,18 @@ UserDetailsService userDetailsService() {
350372

351373
@Bean
352374
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
353-
return http.formLogin((login) -> login.loginPage("/custom-login-page"))
354-
.webAuthn((webauthn) -> webauthn.disableDefaultRegistrationPage(true))
355-
.build();
375+
// @formatter:off
376+
http
377+
.formLogin((login) -> login
378+
.loginPage("/custom-login-page")
379+
)
380+
.webAuthn((authn) -> authn
381+
.rpId("spring.io")
382+
.rpName("spring")
383+
.disableDefaultRegistrationPage(true)
384+
);
385+
// @formatter:on
386+
return http.build();
356387
}
357388

358389
}

0 commit comments

Comments
 (0)