mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-05-31 09:12:14 +00:00
Add noformat blocks around User.withUsername
Find `User.withUsername` calls and protect them against formatting. Issue gh-8945
This commit is contained in:
parent
63b5998fad
commit
6979125ccf
@ -67,12 +67,14 @@ public class UserDetailsMapFactoryBean implements FactoryBean<Collection<UserDet
|
||||
throw new IllegalStateException("The entry with username '" + name
|
||||
+ "' and value '" + property + "' could not be converted to a UserDetails.");
|
||||
}
|
||||
// @formatter:off
|
||||
UserDetails user = User.withUsername(name)
|
||||
.password(attr.getPassword())
|
||||
.disabled(!attr.isEnabled())
|
||||
.authorities(attr.getAuthorities())
|
||||
.build();
|
||||
users.add(user);
|
||||
// @formatter:on
|
||||
} return users;
|
||||
}
|
||||
|
||||
|
@ -114,11 +114,13 @@ public class NamespacePasswordEncoderTests {
|
||||
@Override
|
||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
|
||||
// @formatter:off
|
||||
UserDetails user = User.withUsername("user")
|
||||
.passwordEncoder(encoder::encode)
|
||||
.password("password")
|
||||
.roles("USER")
|
||||
.build();
|
||||
// @formatter:on
|
||||
InMemoryUserDetailsManager uds = new InMemoryUserDetailsManager(user);
|
||||
// @formatter:off
|
||||
auth
|
||||
|
@ -255,10 +255,12 @@ public class EnableWebFluxSecurityTests {
|
||||
static class MapReactiveUserDetailsServiceConfig {
|
||||
@Bean
|
||||
public MapReactiveUserDetailsService userDetailsService() {
|
||||
// @formatter:off
|
||||
return new MapReactiveUserDetailsService(User.withUsername("user")
|
||||
.password("{noop}password")
|
||||
.roles("USER")
|
||||
.build()
|
||||
// @formatter:on
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -92,11 +92,12 @@ public class UserDetailsResourceFactoryBeanTests {
|
||||
|
||||
private void assertLoaded() throws Exception {
|
||||
Collection<UserDetails> users = factory.getObject();
|
||||
|
||||
// @formatter:off
|
||||
UserDetails expectedUser = User.withUsername("user")
|
||||
.password("password")
|
||||
.authorities("ROLE_USER")
|
||||
.build();
|
||||
// @formatter:on
|
||||
assertThat(users).containsExactly(expectedUser);
|
||||
}
|
||||
}
|
||||
|
@ -76,10 +76,12 @@ public class ReactiveUserDetailsServiceAuthenticationManagerTests {
|
||||
|
||||
@Test
|
||||
public void authenticateWhenPasswordNotEqualThenBadCredentials() {
|
||||
// @formatter:off
|
||||
UserDetails user = PasswordEncodedUser.withUsername(this.username)
|
||||
.password(this.password)
|
||||
.roles("USER")
|
||||
.build();
|
||||
// @formatter:on
|
||||
when(repository.findByUsername(user.getUsername())).thenReturn(Mono.just(user));
|
||||
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, this.password + "INVALID");
|
||||
@ -93,10 +95,12 @@ public class ReactiveUserDetailsServiceAuthenticationManagerTests {
|
||||
|
||||
@Test
|
||||
public void authenticateWhenSuccessThenSuccess() {
|
||||
// @formatter:off
|
||||
UserDetails user = PasswordEncodedUser.withUsername(this.username)
|
||||
.password(this.password)
|
||||
.roles("USER")
|
||||
.build();
|
||||
// @formatter:on
|
||||
when(repository.findByUsername(user.getUsername())).thenReturn(Mono.just(user));
|
||||
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
|
||||
|
@ -59,10 +59,12 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
|
||||
@Mock
|
||||
private UserDetailsChecker postAuthenticationChecks;
|
||||
|
||||
// @formatter:off
|
||||
private UserDetails user = User.withUsername("user")
|
||||
.password("password")
|
||||
.roles("USER")
|
||||
.build();
|
||||
// @formatter:on
|
||||
|
||||
private UserDetailsRepositoryReactiveAuthenticationManager manager;
|
||||
|
||||
@ -176,12 +178,13 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
|
||||
@Test(expected = AccountExpiredException.class)
|
||||
public void authenticateWhenAccountExpiredThenException() {
|
||||
this.manager.setPasswordEncoder(this.encoder);
|
||||
|
||||
// @formatter:off
|
||||
UserDetails expiredUser = User.withUsername("user")
|
||||
.password("password")
|
||||
.roles("USER")
|
||||
.accountExpired(true)
|
||||
.build();
|
||||
// @formatter:on
|
||||
when(this.userDetailsService.findByUsername(any())).thenReturn(Mono.just(expiredUser));
|
||||
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
|
||||
@ -193,12 +196,13 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
|
||||
@Test(expected = LockedException.class)
|
||||
public void authenticateWhenAccountLockedThenException() {
|
||||
this.manager.setPasswordEncoder(this.encoder);
|
||||
|
||||
// @formatter:off
|
||||
UserDetails lockedUser = User.withUsername("user")
|
||||
.password("password")
|
||||
.roles("USER")
|
||||
.accountLocked(true)
|
||||
.build();
|
||||
// @formatter:on
|
||||
when(this.userDetailsService.findByUsername(any())).thenReturn(Mono.just(lockedUser));
|
||||
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
|
||||
@ -211,11 +215,13 @@ public class UserDetailsRepositoryReactiveAuthenticationManagerTests {
|
||||
public void authenticateWhenAccountDisabledThenException() {
|
||||
this.manager.setPasswordEncoder(this.encoder);
|
||||
|
||||
// @formatter:off
|
||||
UserDetails disabledUser = User.withUsername("user")
|
||||
.password("password")
|
||||
.roles("USER")
|
||||
.disabled(true)
|
||||
.build();
|
||||
// @formatter:on
|
||||
when(this.userDetailsService.findByUsername(any())).thenReturn(Mono.just(disabledUser));
|
||||
|
||||
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
|
||||
|
@ -27,10 +27,12 @@ import org.junit.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
public class MapReactiveUserDetailsServiceTests {
|
||||
// @formatter:off
|
||||
private static final UserDetails USER_DETAILS = User.withUsername("user")
|
||||
.password("password")
|
||||
.roles("USER")
|
||||
.build();
|
||||
// @formatter:on
|
||||
|
||||
private MapReactiveUserDetailsService users = new MapReactiveUserDetailsService(Arrays.asList(USER_DETAILS));
|
||||
|
||||
|
@ -203,11 +203,13 @@ public class UserTests {
|
||||
|
||||
@Test
|
||||
public void withUsernameWhenPasswordAndPasswordEncoderThenEncodes() {
|
||||
// @formatter:off
|
||||
UserDetails withEncodedPassword = User.withUsername("user")
|
||||
.passwordEncoder(p -> p + "encoded")
|
||||
.password("password")
|
||||
.roles("USER")
|
||||
.build();
|
||||
// @formatter:on
|
||||
|
||||
assertThat(withEncodedPassword.getPassword()).isEqualTo("passwordencoded");
|
||||
}
|
||||
@ -215,12 +217,14 @@ public class UserTests {
|
||||
@Test
|
||||
public void withUsernameWhenPasswordAndPasswordEncoderTwiceThenEncodesOnce() {
|
||||
Function<String, String> encoder = p -> p + "encoded";
|
||||
// @formatter:off
|
||||
UserDetails withEncodedPassword = User.withUsername("user")
|
||||
.passwordEncoder(encoder)
|
||||
.password("password")
|
||||
.passwordEncoder(encoder)
|
||||
.roles("USER")
|
||||
.build();
|
||||
// @formatter:on
|
||||
|
||||
assertThat(withEncodedPassword.getPassword()).isEqualTo("passwordencoded");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user