如何在 Spring 引导中创建自定义 @PreAuthorize MethodSecurity?

How to create custom @PreAuthorize MethodSecurity in Spring Boot?

我正在开发 Spring 引导应用程序。我在控制器方法中检查的那个程序中有一些权限检查。例如,有一些检查方法可以检查是否有重复记录,登录用户是否为会员等。所以我在想的是我需要进入 @PreAuthorize。那么我如何创建自定义方法来执行那些重复验证和成员验证并通过 @PreAuthorize 使用它们。我知道那些重复检查可以在其他任何地方完成,但我个人需要使用 @PreAuthorize。所以有人可以帮助我吗?非常感谢。

您可以使用带有@PreAuthorise 的自定义表达式

@PreAuthorize("isDuplicate(#id)")
@GetMapping("/organizations/{id}")
@ResponseBody
public Organization findOrgById(@PathVariable long id) {
    return organizationRepository.findOne(id);
}


public boolean isDuplicate(Long OrganizationId) {
        // add your logic here to check duplicates or any validation
}

参考https://www.baeldung.com/spring-security-create-new-custom-security-expression了解更多信息