DATACMNS-275 - Introduced getObfuscatedPassword in UserCredentials.

This commit is contained in:
Oliver Gierke
2013-01-29 13:06:47 +01:00
parent 6b49eb4ca5
commit 3e40413ffe
2 changed files with 79 additions and 0 deletions

View File

@@ -79,6 +79,47 @@ public class UserCredentials {
return this.password != null;
}
/**
* Returns the password in obfuscated fashion which means everthing except the first and last character replaced by
* stars. If the password is one or two characters long we'll obfuscate it entirely.
*
* @return the obfuscated password
*/
public String getObfuscatedPassword() {
if (!hasPassword()) {
return null;
}
StringBuilder builder = new StringBuilder();
if (password.length() < 3) {
for (int i = password.length(); i != 0; i--) {
builder.append("*");
}
return builder.toString();
}
builder.append(password.charAt(0));
for (int i = password.length() - 2; i != 0; i--) {
builder.append("*");
}
return builder.append(password.substring(password.length() - 1)).toString();
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("username = [%s], password = [%s]", username, getObfuscatedPassword());
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)