1717package org .springframework .security .authorization ;
1818
1919import java .util .Collection ;
20+ import java .util .HashSet ;
21+ import java .util .List ;
22+ import java .util .Set ;
2023import java .util .function .Supplier ;
2124
2225import 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