【问题标题】:How to create custom @PreAuthorize MethodSecurity in Spring Boot?如何在 Spring Boot 中创建自定义 @PreAuthorize MethodSecurity?
【发布时间】:2021-02-15 22:29:03
【问题描述】:

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

【问题讨论】:

    标签: java authorization


    【解决方案1】:

    您可以使用带有@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

    【讨论】:

    • 非常感谢您的回答和您的宝贵时间。
    猜你喜欢
    • 2019-03-15
    • 2018-02-01
    • 2017-10-03
    • 2014-09-13
    • 2013-01-11
    • 2018-05-28
    • 2016-12-11
    • 2020-03-14
    • 2022-07-21
    相关资源
    最近更新 更多