I have a spring boot project for an e-commerce website. I have implemented Role Based auth for some endpoints but others need extra protection.
For instance this endpoint returns the customer's profile.
@GetMapping("customers/{customerId}")```
I don't want any user with a token to pe able to retrieve any other user's profile. So I need to authenticate that the user requesting this information is the owner of the profile. So in my JWTService i've built this function which verifies that the info extracted from the token matches the info of the requested resource's owner.
``` public boolean isUserAuthorized(Long resourceId, String token) {
String claimedUserName = jwtService.extractUserName(token);
String resourceEmail = userRepository.findById(resourceId)
.orElseThrow(() -> new AuthentificationException("Forbidden"))
.getEmail();
return claimedUserName.equals(resourceEmail);
}```
The question is how can I use this function in an elegant way? I don't think its good practice to insert jwtService in other resource services or stick this function in a controller. Is there an annotation that can help?