I\'m trying to create a Spring Boot 2.1 application. I have created the following rest controller ...
@RestController
@RequestMapping("/api/users")
pub
you can create a service that does the check for you like this:
@Service
public class AuthenticatedUserService {
@Autowired
private IUserRepository userRepository;
public boolean hasId(UUID ID){
String username = SecurityContextHolder.getContext().getAuthentication().getPrincipal().getUsername();
User user = userRepository.findByEmail(username);
return user.getId().equals(id);
}
}
add this to your controller
@Autowired
private AuthenticatedUserService authenticatedUserService;
and replace preauth annotation by this one:
@PreAuthorize("@authenticatedUserService.hasId(#id)")