1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.security.userdetails;
17
18 import java.util.SortedSet;
19 import java.util.TreeSet;
20
21 import org.springframework.security.GrantedAuthority;
22 import org.springframework.util.Assert;
23
24
25
26
27
28
29
30
31
32
33 public class User implements UserDetails {
34
35
36 private static final long serialVersionUID = 1L;
37 private String password;
38 private String username;
39 private GrantedAuthority[] authorities;
40 private boolean accountNonExpired;
41 private boolean accountNonLocked;
42 private boolean credentialsNonExpired;
43 private boolean enabled;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public User(String username, String password, boolean enabled, GrantedAuthority[] authorities)
68 throws IllegalArgumentException {
69 this(username, password, enabled, true, true, authorities);
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 public User(String username, String password, boolean enabled, boolean accountNonExpired,
97 boolean credentialsNonExpired, GrantedAuthority[] authorities)
98 throws IllegalArgumentException {
99 this(username, password, enabled, accountNonExpired, credentialsNonExpired, true, authorities);
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 public User(String username, String password, boolean enabled, boolean accountNonExpired,
126 boolean credentialsNonExpired, boolean accountNonLocked, GrantedAuthority[] authorities)
127 throws IllegalArgumentException {
128 if (((username == null) || "".equals(username)) || (password == null)) {
129 throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
130 }
131
132 this.username = username;
133 this.password = password;
134 this.enabled = enabled;
135 this.accountNonExpired = accountNonExpired;
136 this.credentialsNonExpired = credentialsNonExpired;
137 this.accountNonLocked = accountNonLocked;
138 setAuthorities(authorities);
139 }
140
141
142
143 public boolean equals(Object rhs) {
144 if (!(rhs instanceof User) || (rhs == null)) {
145 return false;
146 }
147
148 User user = (User) rhs;
149
150
151
152 if (user.getAuthorities().length != this.getAuthorities().length) {
153 return false;
154 }
155
156 for (int i = 0; i < this.getAuthorities().length; i++) {
157 if (!this.getAuthorities()[i].equals(user.getAuthorities()[i])) {
158 return false;
159 }
160 }
161
162
163 return (this.getPassword().equals(user.getPassword()) && this.getUsername().equals(user.getUsername())
164 && (this.isAccountNonExpired() == user.isAccountNonExpired())
165 && (this.isAccountNonLocked() == user.isAccountNonLocked())
166 && (this.isCredentialsNonExpired() == user.isCredentialsNonExpired())
167 && (this.isEnabled() == user.isEnabled()));
168 }
169
170 public GrantedAuthority[] getAuthorities() {
171 return authorities;
172 }
173
174 public String getPassword() {
175 return password;
176 }
177
178 public String getUsername() {
179 return username;
180 }
181
182 public int hashCode() {
183 int code = 9792;
184
185 if (this.getAuthorities() != null) {
186 for (int i = 0; i < this.getAuthorities().length; i++) {
187 code = code * (this.getAuthorities()[i].hashCode() % 7);
188 }
189 }
190
191 if (this.getPassword() != null) {
192 code = code * (this.getPassword().hashCode() % 7);
193 }
194
195 if (this.getUsername() != null) {
196 code = code * (this.getUsername().hashCode() % 7);
197 }
198
199 if (this.isAccountNonExpired()) {
200 code = code * -2;
201 }
202
203 if (this.isAccountNonLocked()) {
204 code = code * -3;
205 }
206
207 if (this.isCredentialsNonExpired()) {
208 code = code * -5;
209 }
210
211 if (this.isEnabled()) {
212 code = code * -7;
213 }
214
215 return code;
216 }
217
218 public boolean isAccountNonExpired() {
219 return accountNonExpired;
220 }
221
222 public boolean isAccountNonLocked() {
223 return this.accountNonLocked;
224 }
225
226 public boolean isCredentialsNonExpired() {
227 return credentialsNonExpired;
228 }
229
230 public boolean isEnabled() {
231 return enabled;
232 }
233
234 protected void setAuthorities(GrantedAuthority[] authorities) {
235 Assert.notNull(authorities, "Cannot pass a null GrantedAuthority array");
236
237 SortedSet sorter = new TreeSet();
238 for (int i = 0; i < authorities.length; i++) {
239 Assert.notNull(authorities[i],
240 "Granted authority element " + i + " is null - GrantedAuthority[] cannot contain any null elements");
241 sorter.add(authorities[i]);
242 }
243
244 this.authorities = (GrantedAuthority[]) sorter.toArray(new GrantedAuthority[sorter.size()]);
245 }
246
247 public String toString() {
248 StringBuffer sb = new StringBuffer();
249 sb.append(super.toString()).append(": ");
250 sb.append("Username: ").append(this.username).append("; ");
251 sb.append("Password: [PROTECTED]; ");
252 sb.append("Enabled: ").append(this.enabled).append("; ");
253 sb.append("AccountNonExpired: ").append(this.accountNonExpired).append("; ");
254 sb.append("credentialsNonExpired: ").append(this.credentialsNonExpired).append("; ");
255 sb.append("AccountNonLocked: ").append(this.accountNonLocked).append("; ");
256
257 if (this.getAuthorities() != null) {
258 sb.append("Granted Authorities: ");
259
260 for (int i = 0; i < this.getAuthorities().length; i++) {
261 if (i > 0) {
262 sb.append(", ");
263 }
264
265 sb.append(this.getAuthorities()[i].toString());
266 }
267 } else {
268 sb.append("Not granted any authorities");
269 }
270
271 return sb.toString();
272 }
273 }