|
@@ -16,10 +16,13 @@
|
|
|
|
|
|
package org.springframework.security.authorization;
|
|
package org.springframework.security.authorization;
|
|
|
|
|
|
|
|
+import java.util.Collection;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
import java.util.function.Supplier;
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
|
|
+import org.springframework.security.access.hierarchicalroles.NullRoleHierarchy;
|
|
|
|
+import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.authority.AuthorityUtils;
|
|
import org.springframework.security.core.authority.AuthorityUtils;
|
|
@@ -39,10 +42,23 @@ public final class AuthorityAuthorizationManager<T> implements AuthorizationMana
|
|
|
|
|
|
private final List<GrantedAuthority> authorities;
|
|
private final List<GrantedAuthority> authorities;
|
|
|
|
|
|
|
|
+ private RoleHierarchy roleHierarchy = new NullRoleHierarchy();
|
|
|
|
+
|
|
private AuthorityAuthorizationManager(String... authorities) {
|
|
private AuthorityAuthorizationManager(String... authorities) {
|
|
this.authorities = AuthorityUtils.createAuthorityList(authorities);
|
|
this.authorities = AuthorityUtils.createAuthorityList(authorities);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the {@link RoleHierarchy} to be used. Default is {@link NullRoleHierarchy}.
|
|
|
|
+ * Cannot be null.
|
|
|
|
+ * @param roleHierarchy the {@link RoleHierarchy} to use
|
|
|
|
+ * @since 5.8
|
|
|
|
+ */
|
|
|
|
+ public void setRoleHierarchy(RoleHierarchy roleHierarchy) {
|
|
|
|
+ Assert.notNull(roleHierarchy, "roleHierarchy cannot be null");
|
|
|
|
+ this.roleHierarchy = roleHierarchy;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Creates an instance of {@link AuthorityAuthorizationManager} with the provided
|
|
* Creates an instance of {@link AuthorityAuthorizationManager} with the provided
|
|
* authority.
|
|
* authority.
|
|
@@ -133,7 +149,7 @@ public final class AuthorityAuthorizationManager<T> implements AuthorizationMana
|
|
|
|
|
|
private boolean isAuthorized(Authentication authentication) {
|
|
private boolean isAuthorized(Authentication authentication) {
|
|
Set<String> authorities = AuthorityUtils.authorityListToSet(this.authorities);
|
|
Set<String> authorities = AuthorityUtils.authorityListToSet(this.authorities);
|
|
- for (GrantedAuthority grantedAuthority : authentication.getAuthorities()) {
|
|
|
|
|
|
+ for (GrantedAuthority grantedAuthority : getGrantedAuthorities(authentication)) {
|
|
if (authorities.contains(grantedAuthority.getAuthority())) {
|
|
if (authorities.contains(grantedAuthority.getAuthority())) {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
@@ -141,6 +157,10 @@ public final class AuthorityAuthorizationManager<T> implements AuthorizationMana
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private Collection<? extends GrantedAuthority> getGrantedAuthorities(Authentication authentication) {
|
|
|
|
+ return this.roleHierarchy.getReachableGrantedAuthorities(authentication.getAuthorities());
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public String toString() {
|
|
public String toString() {
|
|
return "AuthorityAuthorizationManager[authorities=" + this.authorities + "]";
|
|
return "AuthorityAuthorizationManager[authorities=" + this.authorities + "]";
|