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