Skip to content

Commit 1c5bf07

Browse files
committed
Refine SimpleGrantedAuthority
1. Deprecate field `role` in favor of `authority` since role is specific authority. 2. Add Javadoc to state that role should be prefixed. Signed-off-by: Yanming Zhou <[email protected]>
1 parent a4f813a commit 1c5bf07

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

core/src/main/java/org/springframework/security/core/authority/SimpleGrantedAuthority.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,31 @@
2727
* {@link org.springframework.security.core.Authentication Authentication} object.
2828
*
2929
* @author Luke Taylor
30+
* @author Yanming Zhou
3031
*/
3132
public final class SimpleGrantedAuthority implements GrantedAuthority {
3233

3334
private static final long serialVersionUID = 620L;
3435

36+
@Deprecated // keep it for JDK deserialization
3537
private final String role;
3638

37-
public SimpleGrantedAuthority(String role) {
38-
Assert.hasText(role, "A granted authority textual representation is required");
39-
this.role = role;
39+
private final String authority;
40+
41+
/**
42+
* Constructs a {@code SimpleGrantedAuthority} using the provided authority.
43+
* @param authority The provided authority such as prefixed role
44+
*/
45+
public SimpleGrantedAuthority(String authority) {
46+
Assert.hasText(authority, "A granted authority textual representation is required");
47+
this.authority = authority;
48+
this.role = authority;
4049
}
4150

4251
@Override
4352
public String getAuthority() {
44-
return this.role;
53+
// authority is null when deserialized from previous version
54+
return (this.authority != null) ? this.authority : this.role;
4555
}
4656

4757
@Override
@@ -50,19 +60,19 @@ public boolean equals(Object obj) {
5060
return true;
5161
}
5262
if (obj instanceof SimpleGrantedAuthority sga) {
53-
return this.role.equals(sga.getAuthority());
63+
return this.getAuthority().equals(sga.getAuthority());
5464
}
5565
return false;
5666
}
5767

5868
@Override
5969
public int hashCode() {
60-
return this.role.hashCode();
70+
return this.getAuthority().hashCode();
6171
}
6272

6373
@Override
6474
public String toString() {
65-
return this.role;
75+
return this.getAuthority();
6676
}
6777

6878
}

0 commit comments

Comments
 (0)