1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.providers.anonymous;
17
18 import org.acegisecurity.GrantedAuthority;
19 import org.acegisecurity.providers.AbstractAuthenticationToken;
20
21 import org.springframework.util.Assert;
22
23 import java.io.Serializable;
24
25
26 /***
27 * Represents an anonymous <code>Authentication</code>.
28 *
29 * @author Ben Alex
30 * @version $Id: AnonymousAuthenticationToken.java,v 1.4 2005/11/17 00:55:50 benalex Exp $
31 */
32 public class AnonymousAuthenticationToken extends AbstractAuthenticationToken
33 implements Serializable {
34
35
36 private Object principal;
37 private GrantedAuthority[] authorities;
38 private boolean authenticated;
39 private int keyHash;
40
41
42
43 /***
44 * Constructor.
45 *
46 * @param key to identify if this object made by an authorised client
47 * @param principal the principal (typically a <code>UserDetails</code>)
48 * @param authorities the authorities granted to the principal
49 *
50 * @throws IllegalArgumentException if a <code>null</code> was passed
51 */
52 public AnonymousAuthenticationToken(String key, Object principal,
53 GrantedAuthority[] authorities) {
54 if ((key == null) || ("".equals(key)) || (principal == null)
55 || "".equals(principal) || (authorities == null)
56 || (authorities.length == 0)) {
57 throw new IllegalArgumentException(
58 "Cannot pass null or empty values to constructor");
59 }
60
61 for (int i = 0; i < authorities.length; i++) {
62 Assert.notNull(authorities[i],
63 "Granted authority element " + i
64 + " is null - GrantedAuthority[] cannot contain any null elements");
65 }
66
67 this.keyHash = key.hashCode();
68 this.principal = principal;
69 this.authorities = authorities;
70 this.authenticated = true;
71 }
72
73 protected AnonymousAuthenticationToken() {
74 throw new IllegalArgumentException("Cannot use default constructor");
75 }
76
77
78
79 public void setAuthenticated(boolean isAuthenticated) {
80 this.authenticated = isAuthenticated;
81 }
82
83 public boolean isAuthenticated() {
84 return this.authenticated;
85 }
86
87 public GrantedAuthority[] getAuthorities() {
88 return this.authorities;
89 }
90
91 /***
92 * Always returns an empty <code>String</code>
93 *
94 * @return an empty String
95 */
96 public Object getCredentials() {
97 return "";
98 }
99
100 public int getKeyHash() {
101 return this.keyHash;
102 }
103
104 public Object getPrincipal() {
105 return this.principal;
106 }
107
108 public boolean equals(Object obj) {
109 if (!super.equals(obj)) {
110 return false;
111 }
112
113 if (obj instanceof AnonymousAuthenticationToken) {
114 AnonymousAuthenticationToken test = (AnonymousAuthenticationToken) obj;
115
116 if (this.getKeyHash() != test.getKeyHash()) {
117 return false;
118 }
119
120 return true;
121 }
122
123 return false;
124 }
125 }