So i've been looking around for a solution but I can't find anything that's exactly what I want, I'm looking to overload a method for different permissions based on the user. I'm using Spring boot and Spring security. For example:
@ResponseBody
@GetMapping("/role")
@Secured("ROLE_ADMIN")
public ResponseEntity<String> areYouAdmin() {
return new ResponseEntity<>("ADMIN YES", HttpStatus.OK);
}
@ResponseBody
@GetMapping("/role")
@Secured("ROLE_USER")
public ResponseEntity<String> areYouUser() {
return new ResponseEntity<>("USER YES", HttpStatus.OK);
}
so for here, if the user is ROLE_USER they would recieve a response of "USER YES" and the ROLE_ADMIN would "ADMIN YES". I know this is is possible with adding a check in the actual method and using the role as a condition but wondering if there is a cleaner approach. A possible usecase here would be if they were searching for a resource and if they were user, it would only display ones where the status was OPEN or something, wheras admin can filter and search by all status. Another one would be if we want to return a different dto, ie admin can view all information about a user.
I also thought about just using different pathing and use my web security config, IE:
.antMatchers("/api/admin").hasRole(ADMIN)
.antMatchers("/api/user").hasRole(USER)
but i really dont want my front end to be "smart", the backend should just display data depending on the role without any special pathing required, or a new servlet. Thanks in advance for any help!