Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何对子属性中包含的ID执行复杂的@PreAuthorize检查?_Spring_Spring Security_Spring Data Jpa_Spring Data_Spring El - Fatal编程技术网

Spring 如何对子属性中包含的ID执行复杂的@PreAuthorize检查?

Spring 如何对子属性中包含的ID执行复杂的@PreAuthorize检查?,spring,spring-security,spring-data-jpa,spring-data,spring-el,Spring,Spring Security,Spring Data Jpa,Spring Data,Spring El,根据这篇伟大的文章 如图所示,您可以使用以下方法将URL id与用户id进行匹配: @PreAuthorize("hasRole('ROLE_ADMIN) or #authUser.id == #userId") 但如果我需要检查userId是否与authUser的子级匹配呢 假设我有实体: public class User { int id; @OneToMany(mappedBy = "owner") Set<Store> stores; } 我想基于userId==

根据这篇伟大的文章

如图所示,您可以使用以下方法将URL id与用户id进行匹配:

@PreAuthorize("hasRole('ROLE_ADMIN) or #authUser.id == #userId")
但如果我需要检查userId是否与authUser的子级匹配呢

假设我有实体:

public class User {
 int id;
 @OneToMany(mappedBy = "owner")
 Set<Store> stores;
}

我想基于userId==authUser进行验证。例如,您可以创建组件

@Component
public class AuthComponent {
    public boolean hasPermission(User user, Long id) {
        // do whatever checks you want here
        return someResult;
    }
}
然后您可以像这样访问SPEL中的组件方法

@PreAuthorize("@authComponent.hasPermission(#authUser, #userId)")

由于SPEL支持通过@

进行bean引用,因此用户结果为空,有什么想法吗?可能组件方法的签名类型与您尝试传递的参数类型不匹配。我可以看看您使用@PreAuthorize的方法吗?在本例中,您提供了在方法签名中包含@CurrentUser authorize的方法。你也有吗?另外,您实际上可以使用内置关键字进行主体/身份验证,这就是问题所在,即使在文档中它也没有引用authUser,那是什么?签名匹配,因为调用了该方法,但参数为null。无论如何,我让它工作了,但只是从方法中抓取用户,更多的代码,但哦,很好。您可以在SPEL中引用@PreAuthorize注释下面方法的参数。在本例中,方法中有authUser作为参数,因此可以使用它们