Guest User

Untitled

a guest
Dec 6th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. @Service
  2. @Transactional
  3. public class ItemService {
  4. ...
  5.  
  6. // Если Item не найден и hasPermission возвращает true, findById выполнится дважды (в @PreAuthorize и в deleteItem).
  7. // Если Item не найден и hasPermission возвращает false, метод deleteItem не выполнится, клиенту вернется 403 вместо 404.
  8. @PreAuthorize("hasPermission(#itemId, 'Item', 'delete') // 403
  9. public void deleteItem(UUID itemId) {
  10. Item item = ItemRepository.findById(itemId).orElseThrow(ItemNotFoundException::new); // 404
  11. ...
  12. }
  13. }
  14.  
  15. @Component
  16. public ItemPermissionEvaluator implements PermissionEvaluator {
  17. ...
  18.  
  19. @Override
  20. public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {
  21. ...
  22. return itemRepository
  23. .findById((UUID) targetId)
  24. .map(item -> item.getOwner().getId().equals(authentication.getPrincipal()))
  25. .orElse(true/false); // true или false, если Item не найден? Может выкинуть ItemNotFoundException здесь?
  26. }
  27. }
Add Comment
Please, Sign In to add comment