浏览代码

Provide toString() method on User.

Ben Alex 20 年之前
父节点
当前提交
0530351f0d

+ 24 - 1
core/src/main/java/org/acegisecurity/userdetails/User.java

@@ -185,6 +185,29 @@ public class User implements UserDetails {
     }
 
     public String toString() {
-        return username;
+        StringBuffer sb = new StringBuffer();
+        sb.append(super.toString() + ": ");
+        sb.append("Username: " + this.username + "; ");
+        sb.append("Password: [PROTECTED]; ");
+        sb.append("Enabled: " + this.enabled + "; ");
+        sb.append("AccountNonExpired: " + this.accountNonExpired + "; ");
+        sb.append("credentialsNonExpired: " + this.credentialsNonExpired + "; ");
+        sb.append("AccountNonLocked: " + this.accountNonLocked + "; ");
+
+        if (this.getAuthorities() != null) {
+            sb.append("Granted Authorities: ");
+
+            for (int i = 0; i < this.getAuthorities().length; i++) {
+                if (i > 0) {
+                    sb.append(", ");
+                }
+
+                sb.append(this.getAuthorities()[i].toString());
+            }
+        } else {
+            sb.append("Not granted any authorities");
+        }
+
+        return sb.toString();
     }
 }

+ 1 - 0
core/src/test/java/org/acegisecurity/providers/dao/UserTests.java

@@ -120,6 +120,7 @@ public class UserTests extends TestCase {
             user.getAuthorities()[0]);
         assertEquals(new GrantedAuthorityImpl("ROLE_TWO"),
             user.getAuthorities()[1]);
+        assertTrue(user.toString().indexOf("marissa") != -1);
     }
 
     public void testUserIsEnabled() throws Exception {

+ 1 - 0
doc/xdocs/changes.xml

@@ -30,6 +30,7 @@
       <action dev="benalex" type="update">UserDetails now advises locked accounts, with corresponding DaoAuthenticationProvider events and enforcement</action>
       <action dev="benalex" type="update">ContextHolderAwareRequestWrapper methods returns null if user is anonymous</action>
       <action dev="benalex" type="update">AbstractBasicAclEntry improved compatibility with Hibernate</action>
+      <action dev="benalex" type="update">User now provides a more useful toString() method</action>
       <action dev="benalex" type="fix">SecurityEnforcementFilter caused NullPointerException when anonymous authentication used with BasicProcessingFilterEntryPoint</action>
       <action dev="benalex" type="fix">FilterChainProxy now supports replacement of ServletRequest and ServetResponse by Filter beans</action>
       <action dev="fbos" type="fix">Corrected Authz parsing of whitespace in GrantedAuthoritys</action>