|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.access.expression; |
| 18 | + |
| 19 | +import java.io.Serializable; |
| 20 | +import java.util.function.Supplier; |
| 21 | + |
| 22 | +import org.jspecify.annotations.Nullable; |
| 23 | + |
| 24 | +import org.springframework.security.access.PermissionEvaluator; |
| 25 | +import org.springframework.security.authorization.AuthorizationManager; |
| 26 | +import org.springframework.security.authorization.AuthorizationManagerFactory; |
| 27 | +import org.springframework.security.authorization.AuthorizationResult; |
| 28 | +import org.springframework.security.authorization.DefaultAuthorizationManagerFactory; |
| 29 | +import org.springframework.security.core.Authentication; |
| 30 | +import org.springframework.util.Assert; |
| 31 | +import org.springframework.util.function.SingletonSupplier; |
| 32 | + |
| 33 | +/** |
| 34 | + * A security expression root object that delegates to {@link AuthorizationManager} |
| 35 | + * instances. |
| 36 | + * |
| 37 | + * @param <T> the type of object that the authorization check is being done on |
| 38 | + * @author Steve Riesenberg |
| 39 | + * @since 7.0 |
| 40 | + */ |
| 41 | +public class AuthorizationManagerSecurityExpressionRoot<T> implements SecurityExpressionOperations { |
| 42 | + |
| 43 | + private static final DefaultAuthorizationManagerFactory<?> DEFAULT_AUTHORIZATION_MANAGER_FACTORY = new DefaultAuthorizationManagerFactory<>(); |
| 44 | + |
| 45 | + private static final PermissionEvaluator DEFAULT_PERMISSION_EVALUATOR = new DenyAllPermissionEvaluator(); |
| 46 | + |
| 47 | + private final Supplier<Authentication> authentication; |
| 48 | + |
| 49 | + private final T object; |
| 50 | + |
| 51 | + private AuthorizationManagerFactory<T> authorizationManagerFactory = defaultAuthorizationManagerFactory(); |
| 52 | + |
| 53 | + private PermissionEvaluator permissionEvaluator = DEFAULT_PERMISSION_EVALUATOR; |
| 54 | + |
| 55 | + public final boolean permitAll = true; |
| 56 | + |
| 57 | + public final boolean denyAll = false; |
| 58 | + |
| 59 | + public final String read = "read"; |
| 60 | + |
| 61 | + public final String write = "write"; |
| 62 | + |
| 63 | + public final String create = "create"; |
| 64 | + |
| 65 | + public final String delete = "delete"; |
| 66 | + |
| 67 | + public final String admin = "administration"; |
| 68 | + |
| 69 | + /** |
| 70 | + * Creates a new instance. |
| 71 | + * @param authentication the {@link Authentication} to use. Cannot be null. |
| 72 | + * @param object the object that the authorization check is being done on |
| 73 | + */ |
| 74 | + public AuthorizationManagerSecurityExpressionRoot(Supplier<Authentication> authentication, T object) { |
| 75 | + this.authentication = SingletonSupplier.of(() -> { |
| 76 | + Authentication value = authentication.get(); |
| 77 | + Assert.notNull(value, "Authentication object cannot be null"); |
| 78 | + return value; |
| 79 | + }); |
| 80 | + this.object = object; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean permitAll() { |
| 85 | + return isGranted(this.authorizationManagerFactory.permitAll()); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public boolean denyAll() { |
| 90 | + return isGranted(this.authorizationManagerFactory.denyAll()); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public boolean hasRole(String role) { |
| 95 | + return isGranted(this.authorizationManagerFactory.hasRole(role)); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public boolean hasAnyRole(String... roles) { |
| 100 | + return isGranted(this.authorizationManagerFactory.hasAnyRole(roles)); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public boolean hasAuthority(String authority) { |
| 105 | + return isGranted(this.authorizationManagerFactory.hasAuthority(authority)); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public boolean hasAnyAuthority(String... authorities) { |
| 110 | + return isGranted(this.authorizationManagerFactory.hasAnyAuthority(authorities)); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public boolean isAnonymous() { |
| 115 | + return isGranted(this.authorizationManagerFactory.anonymous()); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public boolean isAuthenticated() { |
| 120 | + return isGranted(this.authorizationManagerFactory.authenticated()); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public boolean isRememberMe() { |
| 125 | + return isGranted(this.authorizationManagerFactory.rememberMe()); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public boolean isFullyAuthenticated() { |
| 130 | + return isGranted(this.authorizationManagerFactory.fullyAuthenticated()); |
| 131 | + } |
| 132 | + |
| 133 | + private boolean isGranted(AuthorizationManager<T> authorizationManager) { |
| 134 | + AuthorizationResult authorizationResult = authorizationManager.authorize(this.authentication, this.object); |
| 135 | + return (authorizationResult != null && authorizationResult.isGranted()); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public boolean hasPermission(Object target, Object permission) { |
| 140 | + return this.permissionEvaluator.hasPermission(getAuthentication(), target, permission); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public boolean hasPermission(Object targetId, String targetType, Object permission) { |
| 145 | + return this.permissionEvaluator.hasPermission(getAuthentication(), (Serializable) targetId, targetType, |
| 146 | + permission); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Sets the {@link AuthorizationManagerFactory} to use for creating instances of |
| 151 | + * {@link AuthorizationManager}. The default is |
| 152 | + * {@link DefaultAuthorizationManagerFactory}. |
| 153 | + * @param authorizationManagerFactory the {@link AuthorizationManagerFactory} to use |
| 154 | + */ |
| 155 | + public void setAuthorizationManagerFactory(AuthorizationManagerFactory<T> authorizationManagerFactory) { |
| 156 | + Assert.notNull(authorizationManagerFactory, "authorizationManagerFactory cannot be null"); |
| 157 | + this.authorizationManagerFactory = authorizationManagerFactory; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Sets the {@link PermissionEvaluator} to use for evaluating permission checks on |
| 162 | + * domain objects. |
| 163 | + * @param permissionEvaluator the {@link PermissionEvaluator} to use |
| 164 | + */ |
| 165 | + public void setPermissionEvaluator(PermissionEvaluator permissionEvaluator) { |
| 166 | + Assert.notNull(permissionEvaluator, "permissionEvaluator cannot be null"); |
| 167 | + this.permissionEvaluator = permissionEvaluator; |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public Authentication getAuthentication() { |
| 172 | + return this.authentication.get(); |
| 173 | + } |
| 174 | + |
| 175 | + public @Nullable Object getPrincipal() { |
| 176 | + return getAuthentication().getPrincipal(); |
| 177 | + } |
| 178 | + |
| 179 | + @SuppressWarnings("unchecked") |
| 180 | + private static <T> AuthorizationManagerFactory<T> defaultAuthorizationManagerFactory() { |
| 181 | + return (AuthorizationManagerFactory<T>) DEFAULT_AUTHORIZATION_MANAGER_FACTORY; |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments