Skip to content

Commit

Permalink
Provide Runtime Hints for Beans used in Pre/PostAuthorize Expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusdacoregio committed Sep 11, 2024
1 parent 3bb1647 commit 08ce365
Show file tree
Hide file tree
Showing 4 changed files with 553 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler;
import org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
import org.springframework.security.aot.hint.PrePostAuthorizeExpressionBeanHintsRegistrar;
import org.springframework.security.aot.hint.SecurityHintsRegistrar;
import org.springframework.security.authorization.AuthorizationEventPublisher;
import org.springframework.security.authorization.ObservationAuthorizationManager;
import org.springframework.security.authorization.method.AuthorizationManagerAfterMethodInterceptor;
Expand Down Expand Up @@ -191,6 +193,12 @@ static MethodInterceptor postFilterAuthorizationMethodInterceptor(
() -> _prePostMethodSecurityConfiguration.getObject().postFilterMethodInterceptor);
}

@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
static SecurityHintsRegistrar prePostAuthorizeExpressionBeanHintsRegistrar() {
return new PrePostAuthorizeExpressionBeanHintsRegistrar();
}

@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
EnableMethodSecurity annotation = importMetadata.getAnnotations().get(EnableMethodSecurity.class).synthesize();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.security.aot.hint;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.TypeReference;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.expression.spel.SpelNode;
import org.springframework.expression.spel.ast.BeanReference;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authorization.method.AuthorizeReturnObject;
import org.springframework.security.core.annotation.SecurityAnnotationScanner;
import org.springframework.security.core.annotation.SecurityAnnotationScanners;

/**
* A {@link SecurityHintsRegistrar} that scans all beans for methods that use
* {@link PreAuthorize} or {@link PostAuthorize} and registers hints for the beans used
* within the expressions.
*
* @author Marcus da Coregio
* @since 6.4
* @see SecurityHintsAotProcessor
*/
public final class PrePostAuthorizeExpressionBeanHintsRegistrar implements SecurityHintsRegistrar {

private final SecurityAnnotationScanner<PreAuthorize> preAuthorizeScanner = SecurityAnnotationScanners
.requireUnique(PreAuthorize.class);

private final SecurityAnnotationScanner<PostAuthorize> postAuthorizeScanner = SecurityAnnotationScanners
.requireUnique(PostAuthorize.class);

private final SecurityAnnotationScanner<AuthorizeReturnObject> authorizeReturnObjectScanner = SecurityAnnotationScanners
.requireUnique(AuthorizeReturnObject.class);

private final SpelExpressionParser expressionParser = new SpelExpressionParser();

@Override
public void registerHints(RuntimeHints hints, ConfigurableListableBeanFactory beanFactory) {
Set<? extends Class<?>> beans = Arrays.stream(beanFactory.getBeanDefinitionNames())
.map((beanName) -> RegisteredBean.of(beanFactory, beanName).getBeanClass())
.collect(Collectors.toSet());

Set<String> expressions = new HashSet<>();
for (Class<?> bean : beans) {
expressions.addAll(extractSecurityExpressions(bean));
}
Set<String> beanNamesToRegister = new HashSet<>();
for (String expression : expressions) {
beanNamesToRegister.addAll(extractBeanNames(expression));
}
for (String toRegister : beanNamesToRegister) {
Class<?> type = beanFactory.getType(toRegister, false);
if (type == null) {
continue;
}
hints.reflection().registerType(TypeReference.of(type), MemberCategory.INVOKE_DECLARED_METHODS);
}
}

private Set<String> extractSecurityExpressions(Class<?> clazz) {
Set<String> expressions = new HashSet<>();
for (Method method : clazz.getDeclaredMethods()) {
PreAuthorize preAuthorize = this.preAuthorizeScanner.scan(method, clazz);
PostAuthorize postAuthorize = this.postAuthorizeScanner.scan(method, clazz);
if (preAuthorize != null) {
expressions.add(preAuthorize.value());
}
if (postAuthorize != null) {
expressions.add(postAuthorize.value());
}
AuthorizeReturnObject authorizeReturnObject = this.authorizeReturnObjectScanner.scan(method, clazz);
if (authorizeReturnObject != null) {
expressions.addAll(extractSecurityExpressions(method.getReturnType()));
}
}
return expressions;
}

private Set<String> extractBeanNames(String rawExpression) {
SpelExpression expression = this.expressionParser.parseRaw(rawExpression);
SpelNode node = expression.getAST();
Set<String> beanNames = new HashSet<>();
resolveBeanNames(beanNames, node);
return beanNames;
}

private void resolveBeanNames(Set<String> beanNames, SpelNode node) {
if (node instanceof BeanReference br) {
beanNames.add(br.getName());
}
int childCount = node.getChildCount();
if (childCount == 0) {
return;
}
for (int i = 0; i < childCount; i++) {
resolveBeanNames(beanNames, node.getChild(i));
}
}

}
Loading

0 comments on commit 08ce365

Please sign in to comment.