I am using JWT with spring security to handle the authorization for my REST API endpoints. In the following example the endpoint provides information about the user. Let's say I want only ADMIN
users to be able to get the details of all the users, any other user should get only his/her account details. Can anyone think of a more declarative way to implement an equivalent to the following code using annotations or maybe AOP.
@RequestMapping(value = "user/{userId}", method = RequestMethod.GET)
@PreAuthorize("hasAnyRole('USER', 'ADMIN')")
public UserDetailsDto getUserDetails(
@AuthenticationPrincipal JwtUser user,
@PathVariable String userId) {
if (!user.getId().equals(userId) && user.getRole != Role.ADMIN)
throw new BadCredentialsException("Jwt token id and requested ids do not match");
//handle the request
}