简体   繁体   中英

How to create custom @PreAuthorize MethodSecurity in Spring Boot?

I am developing a Spring Boot application. And I have some permission checks in that program that is checked within controller methods. For an example there are some checking methods to check if there is any duplicate records, if that logged in user a member and so on. So what I was thinking is I need to move onto @PreAuthorize . So how can I create custom methods to do those duplicate validations and member validations and used them via @PreAuthorize . I know those duplicate checked can be done in anywhere else but I personally need to use @PreAuthorize . So anybody can help me? I really appreciate that.

You can do this using custom expression with @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
}

refer https://www.baeldung.com/spring-security-create-new-custom-security-expression for more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM