Procházet zdrojové kódy

Improve Error Message for Invalid Properties

Closes gh-3403
Marcus Da Coregio před 4 roky
rodič
revize
19aa44af41

+ 2 - 1
core/src/main/java/org/springframework/security/provisioning/InMemoryUserDetailsManager.java

@@ -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));
 		}
 	}

+ 30 - 1
core/src/test/java/org/springframework/security/provisioning/InMemoryUserDetailsManagerTests.java

@@ -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");
+	}
+
 }