mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-03 17:22:13 +00:00
SEC-177: Add hashCode() method.
This commit is contained in:
parent
c9cee6651c
commit
74de83e5f1
@ -1,4 +1,4 @@
|
|||||||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -16,41 +16,39 @@
|
|||||||
package org.acegisecurity.userdetails;
|
package org.acegisecurity.userdetails;
|
||||||
|
|
||||||
import org.acegisecurity.GrantedAuthority;
|
import org.acegisecurity.GrantedAuthority;
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Models core user information retieved by an {@link UserDetailsService}.
|
* Models core user information retieved by an {@link UserDetailsService}.
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* Implemented with value object semantics (immutable after construction, like a
|
* Implemented with value object semantics (immutable after construction, like
|
||||||
* <code>String</code>). Developers may use this class directly, subclass it,
|
* a <code>String</code>). Developers may use this class directly, subclass
|
||||||
* or write their own {@link UserDetails} implementation from scratch.
|
* it, or write their own {@link UserDetails} implementation from scratch.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @author Ben Alex
|
* @author Ben Alex
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public class User implements UserDetails {
|
public class User implements UserDetails {
|
||||||
|
//~ Instance fields ========================================================
|
||||||
|
|
||||||
// ~ Instance fields
|
// ~ Instance fields
|
||||||
// ========================================================
|
// ========================================================
|
||||||
|
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
private String username;
|
private String username;
|
||||||
|
|
||||||
private GrantedAuthority[] authorities;
|
private GrantedAuthority[] authorities;
|
||||||
|
|
||||||
private boolean accountNonExpired;
|
private boolean accountNonExpired;
|
||||||
|
|
||||||
private boolean accountNonLocked;
|
private boolean accountNonLocked;
|
||||||
|
|
||||||
private boolean credentialsNonExpired;
|
private boolean credentialsNonExpired;
|
||||||
|
|
||||||
private boolean enabled;
|
private boolean enabled;
|
||||||
|
|
||||||
|
//~ Constructors ===========================================================
|
||||||
|
|
||||||
// ~ Constructors
|
// ~ Constructors
|
||||||
// ===========================================================
|
// ===========================================================
|
||||||
|
|
||||||
protected User() {
|
protected User() {
|
||||||
throw new IllegalArgumentException("Cannot use default constructor");
|
throw new IllegalArgumentException("Cannot use default constructor");
|
||||||
}
|
}
|
||||||
@ -59,22 +57,17 @@ public class User implements UserDetails {
|
|||||||
* Construct the <code>User</code> with the details required by {@link
|
* Construct the <code>User</code> with the details required by {@link
|
||||||
* DaoAuthenticationProvider}.
|
* DaoAuthenticationProvider}.
|
||||||
*
|
*
|
||||||
* @param username
|
* @param username the username presented to the
|
||||||
* the username presented to the
|
|
||||||
* <code>DaoAuthenticationProvider</code>
|
* <code>DaoAuthenticationProvider</code>
|
||||||
* @param password
|
* @param password the password that should be presented to the
|
||||||
* the password that should be presented to the
|
|
||||||
* <code>DaoAuthenticationProvider</code>
|
* <code>DaoAuthenticationProvider</code>
|
||||||
* @param enabled
|
* @param enabled set to <code>true</code> if the user is enabled
|
||||||
* set to <code>true</code> if the user is enabled
|
* @param authorities the authorities that should be granted to the caller
|
||||||
* @param authorities
|
* if they presented the correct username and password and the user
|
||||||
* the authorities that should be granted to the caller if they
|
* is enabled
|
||||||
* presented the correct username and password and the user is
|
|
||||||
* enabled
|
|
||||||
*
|
*
|
||||||
* @throws IllegalArgumentException
|
* @throws IllegalArgumentException if a <code>null</code> value was passed
|
||||||
* if a <code>null</code> value was passed either as a
|
* either as a parameter or as an element in the
|
||||||
* parameter or as an element in the
|
|
||||||
* <code>GrantedAuthority[]</code> array
|
* <code>GrantedAuthority[]</code> array
|
||||||
*
|
*
|
||||||
* @deprecated use new constructor with extended properties (this
|
* @deprecated use new constructor with extended properties (this
|
||||||
@ -89,26 +82,21 @@ public class User implements UserDetails {
|
|||||||
* Construct the <code>User</code> with the details required by {@link
|
* Construct the <code>User</code> with the details required by {@link
|
||||||
* DaoAuthenticationProvider}.
|
* DaoAuthenticationProvider}.
|
||||||
*
|
*
|
||||||
* @param username
|
* @param username the username presented to the
|
||||||
* the username presented to the
|
|
||||||
* <code>DaoAuthenticationProvider</code>
|
* <code>DaoAuthenticationProvider</code>
|
||||||
* @param password
|
* @param password the password that should be presented to the
|
||||||
* the password that should be presented to the
|
|
||||||
* <code>DaoAuthenticationProvider</code>
|
* <code>DaoAuthenticationProvider</code>
|
||||||
* @param enabled
|
* @param enabled set to <code>true</code> if the user is enabled
|
||||||
* set to <code>true</code> if the user is enabled
|
* @param accountNonExpired set to <code>true</code> if the account has not
|
||||||
* @param accountNonExpired
|
* expired
|
||||||
* set to <code>true</code> if the account has not expired
|
* @param credentialsNonExpired set to <code>true</code> if the credentials
|
||||||
* @param credentialsNonExpired
|
* have not expired
|
||||||
* set to <code>true</code> if the credentials have not expired
|
* @param authorities the authorities that should be granted to the caller
|
||||||
* @param authorities
|
* if they presented the correct username and password and the user
|
||||||
* the authorities that should be granted to the caller if they
|
* is enabled
|
||||||
* presented the correct username and password and the user is
|
|
||||||
* enabled
|
|
||||||
*
|
*
|
||||||
* @throws IllegalArgumentException
|
* @throws IllegalArgumentException if a <code>null</code> value was passed
|
||||||
* if a <code>null</code> value was passed either as a
|
* either as a parameter or as an element in the
|
||||||
* parameter or as an element in the
|
|
||||||
* <code>GrantedAuthority[]</code> array
|
* <code>GrantedAuthority[]</code> array
|
||||||
*
|
*
|
||||||
* @deprecated use new constructor with extended properties (this
|
* @deprecated use new constructor with extended properties (this
|
||||||
@ -125,28 +113,23 @@ public class User implements UserDetails {
|
|||||||
* Construct the <code>User</code> with the details required by {@link
|
* Construct the <code>User</code> with the details required by {@link
|
||||||
* DaoAuthenticationProvider}.
|
* DaoAuthenticationProvider}.
|
||||||
*
|
*
|
||||||
* @param username
|
* @param username the username presented to the
|
||||||
* the username presented to the
|
|
||||||
* <code>DaoAuthenticationProvider</code>
|
* <code>DaoAuthenticationProvider</code>
|
||||||
* @param password
|
* @param password the password that should be presented to the
|
||||||
* the password that should be presented to the
|
|
||||||
* <code>DaoAuthenticationProvider</code>
|
* <code>DaoAuthenticationProvider</code>
|
||||||
* @param enabled
|
* @param enabled set to <code>true</code> if the user is enabled
|
||||||
* set to <code>true</code> if the user is enabled
|
* @param accountNonExpired set to <code>true</code> if the account has not
|
||||||
* @param accountNonExpired
|
* expired
|
||||||
* set to <code>true</code> if the account has not expired
|
* @param credentialsNonExpired set to <code>true</code> if the credentials
|
||||||
* @param credentialsNonExpired
|
* have not expired
|
||||||
* set to <code>true</code> if the credentials have not expired
|
* @param accountNonLocked set to <code>true</code> if the account is not
|
||||||
* @param accountNonLocked
|
* locked
|
||||||
* set to <code>true</code> if the account is not locked
|
* @param authorities the authorities that should be granted to the caller
|
||||||
* @param authorities
|
* if they presented the correct username and password and the user
|
||||||
* the authorities that should be granted to the caller if they
|
* is enabled
|
||||||
* presented the correct username and password and the user is
|
|
||||||
* enabled
|
|
||||||
*
|
*
|
||||||
* @throws IllegalArgumentException
|
* @throws IllegalArgumentException if a <code>null</code> value was passed
|
||||||
* if a <code>null</code> value was passed either as a
|
* either as a parameter or as an element in the
|
||||||
* parameter or as an element in the
|
|
||||||
* <code>GrantedAuthority[]</code> array
|
* <code>GrantedAuthority[]</code> array
|
||||||
*/
|
*/
|
||||||
public User(String username, String password, boolean enabled,
|
public User(String username, String password, boolean enabled,
|
||||||
@ -167,8 +150,7 @@ public class User implements UserDetails {
|
|||||||
setAuthorities(authorities);
|
setAuthorities(authorities);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ~ Methods
|
//~ Methods ================================================================
|
||||||
// ================================================================
|
|
||||||
|
|
||||||
public boolean equals(Object rhs) {
|
public boolean equals(Object rhs) {
|
||||||
if (!(rhs instanceof User) || (rhs == null)) {
|
if (!(rhs instanceof User) || (rhs == null)) {
|
||||||
@ -194,9 +176,8 @@ public class User implements UserDetails {
|
|||||||
&& this.getUsername().equals(user.getUsername())
|
&& this.getUsername().equals(user.getUsername())
|
||||||
&& (this.isAccountNonExpired() == user.isAccountNonExpired())
|
&& (this.isAccountNonExpired() == user.isAccountNonExpired())
|
||||||
&& (this.isAccountNonLocked() == user.isAccountNonLocked())
|
&& (this.isAccountNonLocked() == user.isAccountNonLocked())
|
||||||
&& (this.isCredentialsNonExpired() == user
|
&& (this.isCredentialsNonExpired() == user.isCredentialsNonExpired())
|
||||||
.isCredentialsNonExpired()) && (this.isEnabled() == user
|
&& (this.isEnabled() == user.isEnabled()));
|
||||||
.isEnabled()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public GrantedAuthority[] getAuthorities() {
|
public GrantedAuthority[] getAuthorities() {
|
||||||
@ -211,6 +192,44 @@ public class User implements UserDetails {
|
|||||||
return username;
|
return username;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ~ Methods
|
||||||
|
// ================================================================
|
||||||
|
public int hashCode() {
|
||||||
|
int code = 9792;
|
||||||
|
|
||||||
|
if (this.getAuthorities() != null) {
|
||||||
|
for (int i = 0; i < this.getAuthorities().length; i++) {
|
||||||
|
code = code * (this.getAuthorities()[i].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() {
|
public boolean isAccountNonExpired() {
|
||||||
return accountNonExpired;
|
return accountNonExpired;
|
||||||
}
|
}
|
||||||
@ -228,13 +247,11 @@ public class User implements UserDetails {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void setAuthorities(GrantedAuthority[] authorities) {
|
protected void setAuthorities(GrantedAuthority[] authorities) {
|
||||||
Assert.notNull(authorities,
|
Assert.notNull(authorities, "Cannot pass a null GrantedAuthority array");
|
||||||
"Cannot pass a null GrantedAuthority array");
|
|
||||||
|
|
||||||
for (int i = 0; i < authorities.length; i++) {
|
for (int i = 0; i < authorities.length; i++) {
|
||||||
Assert.notNull(authorities[i],
|
Assert.notNull(authorities[i],
|
||||||
"Granted authority element "
|
"Granted authority element " + i
|
||||||
+ i
|
|
||||||
+ " is null - GrantedAuthority[] cannot contain any null elements");
|
+ " is null - GrantedAuthority[] cannot contain any null elements");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -248,8 +265,7 @@ public class User implements UserDetails {
|
|||||||
sb.append("Password: [PROTECTED]; ");
|
sb.append("Password: [PROTECTED]; ");
|
||||||
sb.append("Enabled: " + this.enabled + "; ");
|
sb.append("Enabled: " + this.enabled + "; ");
|
||||||
sb.append("AccountNonExpired: " + this.accountNonExpired + "; ");
|
sb.append("AccountNonExpired: " + this.accountNonExpired + "; ");
|
||||||
sb.append("credentialsNonExpired: " + this.credentialsNonExpired
|
sb.append("credentialsNonExpired: " + this.credentialsNonExpired + "; ");
|
||||||
+ "; ");
|
|
||||||
sb.append("AccountNonLocked: " + this.accountNonLocked + "; ");
|
sb.append("AccountNonLocked: " + this.accountNonLocked + "; ");
|
||||||
|
|
||||||
if (this.getAuthorities() != null) {
|
if (this.getAuthorities() != null) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user