فحوص الملكية (Ownership checks)
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
make sure users reach only their own data.
الموضوعات المخطط لها
- Take the user's identity from the authenticated principal, never from the request body or a parameter.
- Option 1: load then compare
order.getCustomerId()with the current user. - Option 2 (preferred): put ownership in the query:
findByIdAndCustomerId(id, customerId). - When it does not belong to you, return 404, not 403, so you do not reveal that it exists.
- Admins may bypass ownership through an explicit, tested path.
تفاصيل المخطط المبدئي
عرض عملي
Order order = orders.findByIdAndCustomerId(id, currentUser.id())
.orElseThrow(() -> new OrderNotFoundException(id));اختبر فهمك
Why return 404 instead of 403 for another customer's order?
أظهر الإجابة
403 would confirm the order exists.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.