Improve Error Message for Invalid Properties

Closes gh-3403
This commit is contained in:
Marcus Da Coregio 2021-06-30 12:23:53 -03:00 committed by Josh Cummings
parent 298068503b
commit 19aa44af41
2 changed files with 32 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2021 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.
@ -79,6 +79,7 @@ public class InMemoryUserDetailsManager implements UserDetailsManager, UserDetai
String name = (String) names.nextElement();
editor.setAsText(users.getProperty(name));
UserAttribute attr = (UserAttribute) editor.getValue();
Assert.notNull(attr, "The entry with username '" + name + "' could not be converted to an UserDetails");
createUser(createUserDetails(name, attr));
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 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.
@ -16,6 +16,8 @@
package org.springframework.security.provisioning;
import java.util.Properties;
import org.junit.Test;
import org.springframework.security.core.userdetails.PasswordEncodedUser;
@ -23,6 +25,7 @@ import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* @author Rob Winch
@ -50,4 +53,30 @@ public class InMemoryUserDetailsManagerTests {
.isEqualTo(newPassword);
}
@Test
public void constructorWhenUserPropertiesThenCreate() {
Properties properties = new Properties();
properties.setProperty("joe", "{noop}joespassword,ROLE_A");
properties.setProperty("bob", "{noop}bobspassword,ROLE_A,ROLE_B");
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(properties);
assertThat(manager.userExists("joe")).isTrue();
assertThat(manager.userExists("bob")).isTrue();
}
@Test
public void constructorWhenUserPropertiesWithEmptyValueThenException() {
Properties properties = new Properties();
properties.setProperty("joe", "");
assertThatIllegalArgumentException().isThrownBy(() -> new InMemoryUserDetailsManager(properties))
.withMessage("The entry with username 'joe' could not be converted to an UserDetails");
}
@Test
public void constructorWhenUserPropertiesNoRolesThenException() {
Properties properties = new Properties();
properties.setProperty("joe", "{noop}joespassword");
assertThatIllegalArgumentException().isThrownBy(() -> new InMemoryUserDetailsManager(properties))
.withMessage("The entry with username 'joe' could not be converted to an UserDetails");
}
}