Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Service
- @Transactional
- public class ItemService {
- ...
- // Если Item не найден и hasPermission возвращает true, findById выполнится дважды (в @PreAuthorize и в deleteItem).
- // Если Item не найден и hasPermission возвращает false, метод deleteItem не выполнится, клиенту вернется 403 вместо 404.
- @PreAuthorize("hasPermission(#itemId, 'Item', 'delete') // 403
- public void deleteItem(UUID itemId) {
- Item item = ItemRepository.findById(itemId).orElseThrow(ItemNotFoundException::new); // 404
- ...
- }
- }
- @Component
- public ItemPermissionEvaluator implements PermissionEvaluator {
- ...
- @Override
- public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {
- ...
- return itemRepository
- .findById((UUID) targetId)
- .map(item -> item.getOwner().getId().equals(authentication.getPrincipal()))
- .orElse(true/false); // true или false, если Item не найден? Может выкинуть ItemNotFoundException здесь?
- }
- }
Add Comment
Please, Sign In to add comment