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