Skip to content

Commit 574318b

Browse files
committed
Support Requiring All Authorities
This update allows AuthoritiesAuthorizationManager to operate in either and or or mode, given a list of authorities.
1 parent dab32cb commit 574318b

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

core/src/main/java/org/springframework/security/authorization/AuthoritiesAuthorizationManager.java

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package org.springframework.security.authorization;
1818

1919
import java.util.Collection;
20+
import java.util.HashSet;
21+
import java.util.List;
22+
import java.util.Set;
2023
import java.util.function.Supplier;
2124

2225
import org.springframework.security.access.hierarchicalroles.NullRoleHierarchy;
@@ -37,6 +40,22 @@ public final class AuthoritiesAuthorizationManager implements AuthorizationManag
3740

3841
private RoleHierarchy roleHierarchy = new NullRoleHierarchy();
3942

43+
private boolean hasAnyAuthority = true;
44+
45+
public AuthoritiesAuthorizationManager() {
46+
47+
}
48+
49+
public static AuthoritiesAuthorizationManager hasAnyAuthority() {
50+
return new AuthoritiesAuthorizationManager();
51+
}
52+
53+
public static AuthoritiesAuthorizationManager hasAllAuthorities() {
54+
AuthoritiesAuthorizationManager manager = new AuthoritiesAuthorizationManager();
55+
manager.hasAnyAuthority = false;
56+
return manager;
57+
}
58+
4059
/**
4160
* Sets the {@link RoleHierarchy} to be used. Default is {@link NullRoleHierarchy}.
4261
* Cannot be null.
@@ -56,25 +75,25 @@ public void setRoleHierarchy(RoleHierarchy roleHierarchy) {
5675
*/
5776
@Override
5877
public AuthorizationResult authorize(Supplier<Authentication> authentication, Collection<String> authorities) {
59-
boolean granted = isGranted(authentication.get(), authorities);
60-
return new AuthorityAuthorizationDecision(granted, AuthorityUtils.createAuthorityList(authorities));
61-
}
62-
63-
private boolean isGranted(Authentication authentication, Collection<String> authorities) {
64-
return authentication != null && isAuthorized(authentication, authorities);
65-
}
66-
67-
private boolean isAuthorized(Authentication authentication, Collection<String> authorities) {
68-
for (GrantedAuthority grantedAuthority : getGrantedAuthorities(authentication)) {
69-
if (authorities.contains(grantedAuthority.getAuthority())) {
70-
return true;
71-
}
78+
Set<String> needed = new HashSet<>(authorities);
79+
for (GrantedAuthority authority : getGrantedAuthorities(authentication.get())) {
80+
needed.remove(authority.getAuthority());
81+
}
82+
if (this.hasAnyAuthority) {
83+
boolean granted = needed.size() < authorities.size();
84+
return new AuthorityAuthorizationDecision(granted, AuthorityUtils.createAuthorityList(authorities));
85+
}
86+
else {
87+
boolean granted = needed.isEmpty();
88+
return new AuthorityAuthorizationDecision(granted, AuthorityUtils.createAuthorityList(needed));
7289
}
73-
return false;
7490
}
7591

76-
private Collection<? extends GrantedAuthority> getGrantedAuthorities(Authentication authentication) {
77-
return this.roleHierarchy.getReachableGrantedAuthorities(authentication.getAuthorities());
92+
private Collection<GrantedAuthority> getGrantedAuthorities(Authentication authentication) {
93+
if (authentication == null) {
94+
return List.of();
95+
}
96+
return new HashSet<>(this.roleHierarchy.getReachableGrantedAuthorities(authentication.getAuthorities()));
7897
}
7998

8099
}

0 commit comments

Comments
 (0)