如何在@PreAuthorize注释中传递调用自定义方法的spring句柄

问题描述 投票:1回答:1

我是Spring安全新手,最近开发了一个需要进行方法级安全性的项目。

我设法像下面这样处理它:

 @Repository
public class EmployeeDaoImpl{
    @PreAuthorize("@mySecurityService.canAdd('ROLE_ADMIN') ")
    public void addEmployee(EmployeeEntity employee) {
        try{
            this.sessionFactory.getCurrentSession().save(employee);         
        }catch(AccessDeniedException e){

        }
    }
}

    @Component
public class MySecurityService {

    public boolean canAdd(String user) {
        System.out.println("Entered has permission..........");
        if(user.equals("ROLE_ADMIN")){
            return false;
        }
        return false;
    }
}

到目前为止一切顺利,一切都很顺利。

我的问题是关于性能,幕后的Spring如何处理在@PreAuthorize()中调用方法,执行任何类型的对象/方法缓存或代理,或者每次通过反射调用方法,以及如何会影响表现吗?

我做了很多搜索,只发现了这个链接,它对我有所帮助,但是你对@PreAuthorize案例有任何进一步的解释吗?

http://spring.io/blog/2007/07/19/debunking-myths-proxies-impact-performance/

希望我的问题很明确,谢谢。

spring-security
1个回答
0
投票

首先,需要解析表达式,然后才能对其进行求值。

作为解析的结果,表达式被转换为SpelNodes树。特别是,MethodReference是负责方法调用的SpelNode

解析部分在PreInvocationAuthorizationAdvice中很好地缓存。

有关MethodReferencecan实施的详细信息,请点击此处:org.springframework.expression.spel.ast.MethodReference org.springframework.expression.spel.ast.MethodReference#getValueInternal(...) org.springframework.expression.spel.support.ReflectiveMethodExecutor

有缓存,java.lang.reflect.Method引用只评估一次(如果目标对象保持相同类型)。

所以这是Spring可以做的最多的事情。进一步的改进需要字节码生成,这在我看来是一种矫枉过正。

© www.soinside.com 2019 - 2024. All rights reserved.