mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-12 13:23:29 +00:00
Allow subclasses to make modifications to GrantedAuthority[].
This commit is contained in:
parent
8c52898dc8
commit
6585c2b391
@ -29,9 +29,6 @@ import org.springframework.util.Assert;
|
|||||||
* a <code>String</code>). Developers may use this class directly, subclass
|
* a <code>String</code>). Developers may use this class directly, subclass
|
||||||
* it, or write their own {@link UserDetails} implementation from scratch.
|
* it, or write their own {@link UserDetails} implementation from scratch.
|
||||||
* </p>
|
* </p>
|
||||||
*
|
|
||||||
* @author Ben Alex
|
|
||||||
* @version $Id$
|
|
||||||
*/
|
*/
|
||||||
public class User implements UserDetails {
|
public class User implements UserDetails {
|
||||||
//~ Instance fields ========================================================
|
//~ Instance fields ========================================================
|
||||||
@ -46,6 +43,10 @@ public class User implements UserDetails {
|
|||||||
|
|
||||||
//~ Constructors ===========================================================
|
//~ Constructors ===========================================================
|
||||||
|
|
||||||
|
protected User() {
|
||||||
|
throw new IllegalArgumentException("Cannot use default constructor");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct the <code>User</code> with the details required by {@link
|
* Construct the <code>User</code> with the details required by {@link
|
||||||
* DaoAuthenticationProvider}.
|
* DaoAuthenticationProvider}.
|
||||||
@ -129,61 +130,22 @@ public class User implements UserDetails {
|
|||||||
boolean accountNonExpired, boolean credentialsNonExpired,
|
boolean accountNonExpired, boolean credentialsNonExpired,
|
||||||
boolean accountNonLocked, GrantedAuthority[] authorities)
|
boolean accountNonLocked, GrantedAuthority[] authorities)
|
||||||
throws IllegalArgumentException {
|
throws IllegalArgumentException {
|
||||||
if (((username == null) || "".equals(username)) || (password == null)
|
if (((username == null) || "".equals(username)) || (password == null)) {
|
||||||
|| (authorities == null)) {
|
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Cannot pass null or empty values to constructor");
|
"Cannot pass null or empty values to constructor");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < authorities.length; i++) {
|
|
||||||
Assert.notNull(authorities[i],
|
|
||||||
"Granted authority element " + i
|
|
||||||
+ " is null - GrantedAuthority[] cannot contain any null elements");
|
|
||||||
}
|
|
||||||
|
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.enabled = enabled;
|
this.enabled = enabled;
|
||||||
this.authorities = authorities;
|
|
||||||
this.accountNonExpired = accountNonExpired;
|
this.accountNonExpired = accountNonExpired;
|
||||||
this.credentialsNonExpired = credentialsNonExpired;
|
this.credentialsNonExpired = credentialsNonExpired;
|
||||||
this.accountNonLocked = accountNonLocked;
|
this.accountNonLocked = accountNonLocked;
|
||||||
}
|
setAuthorities(authorities);
|
||||||
|
|
||||||
protected User() {
|
|
||||||
throw new IllegalArgumentException("Cannot use default constructor");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//~ Methods ================================================================
|
//~ Methods ================================================================
|
||||||
|
|
||||||
public boolean isAccountNonExpired() {
|
|
||||||
return accountNonExpired;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isAccountNonLocked() {
|
|
||||||
return this.accountNonLocked;
|
|
||||||
}
|
|
||||||
|
|
||||||
public GrantedAuthority[] getAuthorities() {
|
|
||||||
return authorities;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isCredentialsNonExpired() {
|
|
||||||
return credentialsNonExpired;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isEnabled() {
|
|
||||||
return enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPassword() {
|
|
||||||
return password;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUsername() {
|
|
||||||
return username;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean equals(Object rhs) {
|
public boolean equals(Object rhs) {
|
||||||
if (!(rhs instanceof User) || (rhs == null)) {
|
if (!(rhs instanceof User) || (rhs == null)) {
|
||||||
return false;
|
return false;
|
||||||
@ -211,6 +173,46 @@ public class User implements UserDetails {
|
|||||||
&& (this.isEnabled() == user.isEnabled()));
|
&& (this.isEnabled() == user.isEnabled()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public GrantedAuthority[] getAuthorities() {
|
||||||
|
return authorities;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isAccountNonExpired() {
|
||||||
|
return accountNonExpired;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isAccountNonLocked() {
|
||||||
|
return this.accountNonLocked;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCredentialsNonExpired() {
|
||||||
|
return credentialsNonExpired;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setAuthorities(GrantedAuthority[] authorities) {
|
||||||
|
Assert.notNull(authorities, "Cannot pass a null GrantedAuthority array");
|
||||||
|
|
||||||
|
for (int i = 0; i < authorities.length; i++) {
|
||||||
|
Assert.notNull(authorities[i],
|
||||||
|
"Granted authority element " + i
|
||||||
|
+ " is null - GrantedAuthority[] cannot contain any null elements");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.authorities = authorities;
|
||||||
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
sb.append(super.toString() + ": ");
|
sb.append(super.toString() + ": ");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user