|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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.authorization.object; |
| 18 | + |
| 19 | +import java.lang.reflect.Array; |
| 20 | +import java.lang.reflect.Modifier; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.Iterator; |
| 24 | +import java.util.LinkedHashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Optional; |
| 28 | +import java.util.stream.Stream; |
| 29 | + |
| 30 | +import org.springframework.aop.Advisor; |
| 31 | +import org.springframework.aop.framework.ProxyFactory; |
| 32 | +import org.springframework.core.annotation.AnnotationAwareOrderComparator; |
| 33 | +import org.springframework.security.authorization.method.AuthorizationAdvisor; |
| 34 | +import org.springframework.util.ClassUtils; |
| 35 | + |
| 36 | +/** |
| 37 | + * A proxy factory for applying authorization advice to an arbitrary object. |
| 38 | + * |
| 39 | + * <p> |
| 40 | + * For example, consider a non-Spring-managed object {@code Foo}: <pre> |
| 41 | + * class Foo { |
| 42 | + * @PreAuthorize("hasAuthority('bar:read')") |
| 43 | + * String bar() { ... } |
| 44 | + * } |
| 45 | + * </pre> |
| 46 | + * |
| 47 | + * Use {@link AuthorizationProxyFactory} to wrap the instance in Spring Security's |
| 48 | + * {@link org.springframework.security.access.prepost.PreAuthorize} method interceptor |
| 49 | + * like so: |
| 50 | + * |
| 51 | + * <pre> |
| 52 | + * AuthorizationManagerBeforeMethodInterceptor preAuthorize = AuthorizationManagerBeforeMethodInterceptor.preAuthorize(); |
| 53 | + * AuthorizationProxyFactory proxyFactory = new AuthorizationProxyFactory(preAuthorize); |
| 54 | + * Foo foo = new Foo(); |
| 55 | + * foo.bar(); // passes |
| 56 | + * Foo securedFoo = proxyFactory.proxy(foo); |
| 57 | + * securedFoo.bar(); // access denied! |
| 58 | + * </pre> |
| 59 | + * |
| 60 | + * @author Josh Cummings |
| 61 | + * @since 6.3 |
| 62 | + */ |
| 63 | +public final class AuthorizationProxyFactory { |
| 64 | + |
| 65 | + private final Collection<AuthorizationAdvisor> advisors; |
| 66 | + |
| 67 | + public AuthorizationProxyFactory(AuthorizationAdvisor... advisors) { |
| 68 | + this.advisors = List.of(advisors); |
| 69 | + } |
| 70 | + |
| 71 | + public AuthorizationProxyFactory(Collection<AuthorizationAdvisor> advisors) { |
| 72 | + this.advisors = List.copyOf(advisors); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Create a new {@link AuthorizationProxyFactory} that includes the given advisors in |
| 77 | + * addition to any advisors {@code this} instance already has. |
| 78 | + * |
| 79 | + * <p> |
| 80 | + * All advisors are re-sorted by their advisor order. |
| 81 | + * @param advisors the advisors to add |
| 82 | + * @return a new {@link AuthorizationProxyFactory} instance |
| 83 | + */ |
| 84 | + public AuthorizationProxyFactory withAdvisors(AuthorizationAdvisor... advisors) { |
| 85 | + List<AuthorizationAdvisor> merged = new ArrayList<>(this.advisors.size() + 1); |
| 86 | + merged.addAll(this.advisors); |
| 87 | + merged.addAll(List.of(advisors)); |
| 88 | + AnnotationAwareOrderComparator.sort(merged); |
| 89 | + return new AuthorizationProxyFactory(merged); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Proxy an object to enforce authorization advice. |
| 94 | + * |
| 95 | + * <p> |
| 96 | + * Proxies any instance of a non-final class or a class that implements more than one |
| 97 | + * interface. |
| 98 | + * |
| 99 | + * <p> |
| 100 | + * If {@code target} is an {@link Iterator}, {@link Collection}, {@link Array}, |
| 101 | + * {@link Map}, {@link Stream}, or {@link Optional}, then the element or value type is |
| 102 | + * proxied. |
| 103 | + * |
| 104 | + * <p> |
| 105 | + * If {@code target} is a {@link Class}, then {@link ProxyFactory#getProxyClass} is |
| 106 | + * invoked instead. |
| 107 | + * @param target the instance to proxy |
| 108 | + * @return the proxied instance |
| 109 | + */ |
| 110 | + public Object proxy(Object target) { |
| 111 | + if (target == null) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + if (target instanceof Class<?> targetClass) { |
| 115 | + return (targetClass.isInterface()) ? targetClass : proxyClass(targetClass); |
| 116 | + } |
| 117 | + if (ClassUtils.isSimpleValueType(target.getClass())) { |
| 118 | + return target; |
| 119 | + } |
| 120 | + if (target instanceof Iterator<?> iterator) { |
| 121 | + return proxyIterator(iterator); |
| 122 | + } |
| 123 | + if (target instanceof Collection<?> collection) { |
| 124 | + return proxyCollection(collection); |
| 125 | + } |
| 126 | + if (target.getClass().isArray()) { |
| 127 | + return proxyArray((Object[]) target); |
| 128 | + } |
| 129 | + if (target instanceof Map<?, ?> map) { |
| 130 | + return proxyMap(map); |
| 131 | + } |
| 132 | + if (target instanceof Stream<?> stream) { |
| 133 | + return proxyStream(stream); |
| 134 | + } |
| 135 | + if (target instanceof Optional<?> optional) { |
| 136 | + return proxyOptional(optional); |
| 137 | + } |
| 138 | + ProxyFactory factory = new ProxyFactory(target); |
| 139 | + for (Advisor advisor : this.advisors) { |
| 140 | + factory.addAdvisors(advisor); |
| 141 | + } |
| 142 | + factory.setProxyTargetClass(!Modifier.isFinal(target.getClass().getModifiers())); |
| 143 | + return factory.getProxy(); |
| 144 | + } |
| 145 | + |
| 146 | + private Class<?> proxyClass(Class<?> targetClass) { |
| 147 | + ProxyFactory factory = new ProxyFactory(); |
| 148 | + factory.setTargetClass(targetClass); |
| 149 | + factory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass)); |
| 150 | + factory.setProxyTargetClass(!Modifier.isFinal(targetClass.getModifiers())); |
| 151 | + for (Advisor advisor : this.advisors) { |
| 152 | + factory.addAdvisors(advisor); |
| 153 | + } |
| 154 | + return factory.getProxyClass(getClass().getClassLoader()); |
| 155 | + } |
| 156 | + |
| 157 | + private Iterator<?> proxyIterator(Iterator<?> iterator) { |
| 158 | + return new Iterator<>() { |
| 159 | + @Override |
| 160 | + public boolean hasNext() { |
| 161 | + return iterator.hasNext(); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public Object next() { |
| 166 | + return proxy(iterator.next()); |
| 167 | + } |
| 168 | + }; |
| 169 | + } |
| 170 | + |
| 171 | + private <T> Collection<T> proxyCollection(Collection<T> collection) { |
| 172 | + Collection<T> proxies = new ArrayList<>(collection.size()); |
| 173 | + for (T toProxy : collection) { |
| 174 | + proxies.add((T) proxy(toProxy)); |
| 175 | + } |
| 176 | + collection.clear(); |
| 177 | + collection.addAll(proxies); |
| 178 | + return proxies; |
| 179 | + } |
| 180 | + |
| 181 | + private Object[] proxyArray(Object[] objects) { |
| 182 | + List<Object> retain = new ArrayList<>(objects.length); |
| 183 | + for (Object object : objects) { |
| 184 | + retain.add(proxy(object)); |
| 185 | + } |
| 186 | + Object[] proxies = (Object[]) Array.newInstance(objects.getClass().getComponentType(), retain.size()); |
| 187 | + for (int i = 0; i < retain.size(); i++) { |
| 188 | + proxies[i] = retain.get(i); |
| 189 | + } |
| 190 | + return proxies; |
| 191 | + } |
| 192 | + |
| 193 | + private <K, V> Map<K, V> proxyMap(Map<K, V> entries) { |
| 194 | + Map<K, V> proxies = new LinkedHashMap<>(entries.size()); |
| 195 | + for (Map.Entry<K, V> entry : entries.entrySet()) { |
| 196 | + proxies.put(entry.getKey(), (V) proxy(entry.getValue())); |
| 197 | + } |
| 198 | + entries.clear(); |
| 199 | + entries.putAll(proxies); |
| 200 | + return entries; |
| 201 | + } |
| 202 | + |
| 203 | + private Stream<?> proxyStream(Stream<?> stream) { |
| 204 | + return stream.map(this::proxy).onClose(stream::close); |
| 205 | + } |
| 206 | + |
| 207 | + private Optional<?> proxyOptional(Optional<?> optional) { |
| 208 | + return optional.map(this::proxy); |
| 209 | + } |
| 210 | + |
| 211 | +} |
0 commit comments