mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-07 19:22:14 +00:00
Added trimming of whitespace to tokens and use of Springs StringUtils.hasText() to check for content in the string passed to setAsText.
This commit is contained in:
parent
8e1549e399
commit
c89d4a8add
@ -33,14 +33,12 @@ public class UserAttributeEditor extends PropertyEditorSupport {
|
|||||||
//~ Methods ================================================================
|
//~ Methods ================================================================
|
||||||
|
|
||||||
public void setAsText(String s) throws IllegalArgumentException {
|
public void setAsText(String s) throws IllegalArgumentException {
|
||||||
if ((s == null) || "".equals(s)) {
|
if (StringUtils.hasText(s)) {
|
||||||
setValue(null);
|
|
||||||
} else {
|
|
||||||
String[] tokens = StringUtils.commaDelimitedListToStringArray(s);
|
String[] tokens = StringUtils.commaDelimitedListToStringArray(s);
|
||||||
UserAttribute userAttrib = new UserAttribute();
|
UserAttribute userAttrib = new UserAttribute();
|
||||||
|
|
||||||
for (int i = 0; i < tokens.length; i++) {
|
for (int i = 0; i < tokens.length; i++) {
|
||||||
String currentToken = tokens[i];
|
String currentToken = tokens[i].trim();
|
||||||
|
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
userAttrib.setPassword(currentToken);
|
userAttrib.setPassword(currentToken);
|
||||||
@ -61,6 +59,8 @@ public class UserAttributeEditor extends PropertyEditorSupport {
|
|||||||
} else {
|
} else {
|
||||||
setValue(null);
|
setValue(null);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
setValue(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -123,4 +123,15 @@ public class UserAttributeEditorTests extends TestCase {
|
|||||||
UserAttribute user = (UserAttribute) editor.getValue();
|
UserAttribute user = (UserAttribute) editor.getValue();
|
||||||
assertTrue(user == null);
|
assertTrue(user == null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testCorrectOperationWithTrailingSpaces() {
|
||||||
|
UserAttributeEditor editor = new UserAttributeEditor();
|
||||||
|
editor.setAsText("password ,ROLE_ONE,ROLE_TWO ");
|
||||||
|
|
||||||
|
UserAttribute user = (UserAttribute) editor.getValue();
|
||||||
|
assertEquals("password", user.getPassword());
|
||||||
|
assertEquals(2, user.getAuthorities().length);
|
||||||
|
assertEquals("ROLE_ONE", user.getAuthorities()[0].getAuthority());
|
||||||
|
assertEquals("ROLE_TWO", user.getAuthorities()[1].getAuthority());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user