diff --git a/core/src/main/java/org/springframework/security/core/userdetails/User.java b/core/src/main/java/org/springframework/security/core/userdetails/User.java
index 5932171ab2..a1216af40e 100644
--- a/core/src/main/java/org/springframework/security/core/userdetails/User.java
+++ b/core/src/main/java/org/springframework/security/core/userdetails/User.java
@@ -81,7 +81,7 @@ public class User implements UserDetails {
* GrantedAuthority collection
*/
public User(String username, String password, boolean enabled, boolean accountNonExpired,
- boolean credentialsNonExpired, boolean accountNonLocked, Collection authorities) {
+ boolean credentialsNonExpired, boolean accountNonLocked, Collection extends GrantedAuthority> authorities) {
if (((username == null) || "".equals(username)) || (password == null)) {
throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
@@ -98,27 +98,6 @@ public class User implements UserDetails {
//~ Methods ========================================================================================================
- public boolean equals(Object rhs) {
- if (!(rhs instanceof User) || (rhs == null)) {
- return false;
- }
-
- User user = (User) rhs;
-
- // We rely on constructor to guarantee any User has non-null
- // authorities
- if (!authorities.equals(user.authorities)) {
- return false;
- }
-
- // We rely on constructor to guarantee non-null username and password
- return (this.getPassword().equals(user.getPassword()) && this.getUsername().equals(user.getUsername())
- && (this.isAccountNonExpired() == user.isAccountNonExpired())
- && (this.isAccountNonLocked() == user.isAccountNonLocked())
- && (this.isCredentialsNonExpired() == user.isCredentialsNonExpired())
- && (this.isEnabled() == user.isEnabled()));
- }
-
public Collection getAuthorities() {
return authorities;
}
@@ -131,40 +110,6 @@ public class User implements UserDetails {
return username;
}
- public int hashCode() {
- int code = 9792;
-
- for (GrantedAuthority authority : getAuthorities()) {
- code = code * (authority.hashCode() % 7);
- }
-
- if (this.getPassword() != null) {
- code = code * (this.getPassword().hashCode() % 7);
- }
-
- if (this.getUsername() != null) {
- code = code * (this.getUsername().hashCode() % 7);
- }
-
- if (this.isAccountNonExpired()) {
- code = code * -2;
- }
-
- if (this.isAccountNonLocked()) {
- code = code * -3;
- }
-
- if (this.isCredentialsNonExpired()) {
- code = code * -5;
- }
-
- if (this.isEnabled()) {
- code = code * -7;
- }
-
- return code;
- }
-
public boolean isAccountNonExpired() {
return accountNonExpired;
}
@@ -181,7 +126,7 @@ public class User implements UserDetails {
return enabled;
}
- private static SortedSet sortAuthorities(Collection authorities) {
+ private static SortedSet sortAuthorities(Collection extends GrantedAuthority> authorities) {
Assert.notNull(authorities, "Cannot pass a null GrantedAuthority collection");
// Ensure array iteration order is predictable (as per UserDetails.getAuthorities() contract and SEC-717)
SortedSet sortedAuthorities =
@@ -211,7 +156,64 @@ public class User implements UserDetails {
}
}
+ @Override
+ public boolean equals(Object rhs) {
+ if (!(rhs instanceof User) || (rhs == null)) {
+ return false;
+ }
+
+ User user = (User) rhs;
+
+ // We rely on constructor to guarantee any User has non-null
+ // authorities
+ if (!authorities.equals(user.authorities)) {
+ return false;
+ }
+
+ // We rely on constructor to guarantee non-null username and password
+ return (this.getPassword().equals(user.getPassword()) && this.getUsername().equals(user.getUsername())
+ && (this.isAccountNonExpired() == user.isAccountNonExpired())
+ && (this.isAccountNonLocked() == user.isAccountNonLocked())
+ && (this.isCredentialsNonExpired() == user.isCredentialsNonExpired())
+ && (this.isEnabled() == user.isEnabled()));
+ }
+
+ @Override
+ public int hashCode() {
+ int code = 9792;
+
+ for (GrantedAuthority authority : getAuthorities()) {
+ code = code * (authority.hashCode() % 7);
+ }
+
+ if (this.getPassword() != null) {
+ code = code * (this.getPassword().hashCode() % 7);
+ }
+
+ if (this.getUsername() != null) {
+ code = code * (this.getUsername().hashCode() % 7);
+ }
+
+ if (this.isAccountNonExpired()) {
+ code = code * -2;
+ }
+
+ if (this.isAccountNonLocked()) {
+ code = code * -3;
+ }
+
+ if (this.isCredentialsNonExpired()) {
+ code = code * -5;
+ }
+
+ if (this.isEnabled()) {
+ code = code * -7;
+ }
+
+ return code;
+ }
+ @Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(super.toString()).append(": ");