View Javadoc
1   package cn.home1.oss.lib.security.internal;
2   
3   import lombok.AccessLevel;
4   import lombok.Getter;
5   import lombok.NoArgsConstructor;
6   import lombok.NonNull;
7   import lombok.Setter;
8   
9   import org.springframework.security.core.GrantedAuthority;
10  import org.springframework.security.core.SpringSecurityCoreVersion;
11  import org.springframework.security.core.authority.SimpleGrantedAuthority;
12  import org.springframework.util.Assert;
13  
14  import javax.xml.bind.annotation.XmlRootElement;
15  import javax.xml.bind.annotation.XmlValue;
16  
17  /**
18   * Created by Meifans on 17/1/13.
19   */
20  @XmlRootElement(name = "authority")
21  @NoArgsConstructor(access = AccessLevel.PRIVATE)
22  @Setter(AccessLevel.PRIVATE)
23  @Getter
24  public class BaseGrantedAuthority implements GrantedAuthority {
25  
26    private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
27  
28    @NonNull
29    @XmlValue
30    private String authority;
31  
32    public BaseGrantedAuthority(final String role) {
33      Assert.hasText(role, "A granted authority textual representation is required");
34      this.authority = role;
35    }
36  
37    @Override
38    public String toString() {
39      return this.authority;
40    }
41  
42    public boolean equals(final Object obj) {
43      final boolean result;
44  
45      if (this != obj) {
46        if (obj instanceof BaseGrantedAuthority) {
47          result = this.authority.equals(((BaseGrantedAuthority) obj).getAuthority());
48        } else if (obj instanceof SimpleGrantedAuthority) {
49          result = this.authority.equals(((SimpleGrantedAuthority) obj).getAuthority());
50        } else {
51          result = false;
52        }
53      } else {
54        result = false;
55      }
56  
57      return result;
58    }
59  
60    public int hashCode() {
61      return this.authority.hashCode();
62    }
63  }