1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.security.providers;
17
18 import java.security.Principal;
19
20 import org.springframework.security.Authentication;
21 import org.springframework.security.GrantedAuthority;
22 import org.springframework.security.userdetails.UserDetails;
23 import org.springframework.util.Assert;
24
25
26
27
28
29
30
31
32
33 public abstract class AbstractAuthenticationToken implements Authentication {
34
35
36 private Object details;
37 private GrantedAuthority[] authorities;
38 private boolean authenticated = false;
39
40
41
42
43
44
45
46
47
48
49
50 public AbstractAuthenticationToken() {
51 }
52
53
54
55
56
57
58
59
60
61
62
63 public AbstractAuthenticationToken(GrantedAuthority[] authorities) {
64 if (authorities != null) {
65 for (int i = 0; i < authorities.length; i++) {
66 Assert.notNull(authorities[i],
67 "Granted authority element " + i + " is null - GrantedAuthority[] cannot contain any null elements");
68 }
69 }
70
71 this.authorities = authorities;
72 }
73
74
75
76 public boolean equals(Object obj) {
77 if (obj instanceof AbstractAuthenticationToken) {
78 AbstractAuthenticationToken test = (AbstractAuthenticationToken) obj;
79
80 if (!((this.getAuthorities() == null) && (test.getAuthorities() == null))) {
81 if ((this.getAuthorities() == null) || (test.getAuthorities() == null)) {
82 return false;
83 }
84
85 if (this.getAuthorities().length != test.getAuthorities().length) {
86 return false;
87 }
88
89 for (int i = 0; i < this.getAuthorities().length; i++) {
90 if (!this.getAuthorities()[i].equals(test.getAuthorities()[i])) {
91 return false;
92 }
93 }
94 }
95
96 if ((this.details == null) && (test.getDetails() != null)) {
97 return false;
98 }
99
100 if ((this.details != null) && (test.getDetails() == null)) {
101 return false;
102 }
103
104 if ((this.details != null) && (!this.details.equals(test.getDetails()))) {
105 return false;
106 }
107
108 if ((this.getCredentials() == null) && (test.getCredentials() != null)) {
109 return false;
110 }
111
112 if ((this.getCredentials() != null) && !this.getCredentials().equals(test.getCredentials())) {
113 return false;
114 }
115
116 if (this.getPrincipal() == null && test.getPrincipal() != null) {
117 return false;
118 }
119
120 if (this.getPrincipal() != null && !this.getPrincipal().equals(test.getPrincipal())) {
121 return false;
122 }
123
124 return this.isAuthenticated() == test.isAuthenticated();
125 }
126
127 return false;
128 }
129
130 public GrantedAuthority[] getAuthorities() {
131 if (authorities == null) {
132 return null;
133 }
134
135 GrantedAuthority[] copy = new GrantedAuthority[authorities.length];
136 System.arraycopy(authorities, 0, copy, 0, authorities.length);
137
138 return copy;
139 }
140
141 public Object getDetails() {
142 return details;
143 }
144
145 public String getName() {
146 if (this.getPrincipal() instanceof UserDetails) {
147 return ((UserDetails) this.getPrincipal()).getUsername();
148 }
149
150 if (getPrincipal() instanceof Principal) {
151 return ((Principal)getPrincipal()).getName();
152 }
153
154 return (this.getPrincipal() == null) ? "" : this.getPrincipal().toString();
155 }
156
157 public int hashCode() {
158 int code = 31;
159
160
161 GrantedAuthority[] authorities = this.getAuthorities();
162
163 if (authorities != null) {
164 for (int i = 0; i < authorities.length; i++) {
165 code ^= authorities[i].hashCode();
166 }
167 }
168
169 if (this.getPrincipal() != null) {
170 code ^= this.getPrincipal().hashCode();
171 }
172
173 if (this.getCredentials() != null) {
174 code ^= this.getCredentials().hashCode();
175 }
176
177 if (this.getDetails() != null) {
178 code ^= this.getDetails().hashCode();
179 }
180
181 if (this.isAuthenticated()) {
182 code ^= -37;
183 }
184
185 return code;
186 }
187
188 public boolean isAuthenticated() {
189 return authenticated;
190 }
191
192 public void setAuthenticated(boolean authenticated) {
193 this.authenticated = authenticated;
194 }
195
196 public void setDetails(Object details) {
197 this.details = details;
198 }
199
200 public String toString() {
201 StringBuffer sb = new StringBuffer();
202 sb.append(super.toString()).append(": ");
203 sb.append("Principal: ").append(this.getPrincipal()).append("; ");
204 sb.append("Password: [PROTECTED]; ");
205 sb.append("Authenticated: ").append(this.isAuthenticated()).append("; ");
206 sb.append("Details: ").append(this.getDetails()).append("; ");
207
208 if (this.getAuthorities() != null) {
209 sb.append("Granted Authorities: ");
210
211 for (int i = 0; i < this.getAuthorities().length; i++) {
212 if (i > 0) {
213 sb.append(", ");
214 }
215
216 sb.append(this.getAuthorities()[i].toString());
217 }
218 } else {
219 sb.append("Not granted any authorities");
220 }
221
222 return sb.toString();
223 }
224 }