Secure Customer Orders API
You are maintaining a customer orders service where users can place and view their own purchase records. Recent security audits revealed that users can access and modify orders belonging to other accounts by simply guessing or iterating through order IDs. Your task is to secure the application against these authorization bypasses.
Endpoints
POST /api/auth/login: Acceptsusernameandpasswordin JSON. Returns 200 OK withtoken.GET /api/orders: Returns 200 OK with a list of orders belonging exclusively to the authenticated user.GET /api/orders/{id}: Returns 200 OK with the order details if it belongs to the authenticated user, or 403 Forbidden / 404 Not Found if it does not.PUT /api/orders/{id}: Updates an order if it belongs to the authenticated user. Returns 200 OK or 403 Forbidden.
Security Requirements
- A user can only view and edit their own orders. Attempting to access another user's order must be denied with a 403 or 404 status.
- The authentication token must be verified on every protected request via the authorization header.
- The store has
alicewith passwordsecretaliceowning order1, andbobwith passwordsecretbobowning order2.
The setup
The API works for honest clients. Close the holes the security requirements describe without breaking what works.
Spring Boot cannot run in a browser. When you submit, a reviewer traces every scenario through your code.
- src/main/java/com/example/api/ApiApplication.java
- src/main/java/com/example/api/store/Store.java
- src/main/java/com/example/api/security/AuthInterceptor.java
- src/main/java/com/example/api/web/OrderController.java
- src/main/java/com/example/api/web/AuthController.java
- src/main/java/com/example/api/service/OrderService.java
Locked files are given finished for this task and open read-only.
There is no database: the API keeps its data in memory. Every scenario starts from a fresh app with the seed data.
Follow-up: How would you implement role-based access control alongside resource ownership for admin users?
Do not add external dependencies. Use only Spring Boot built-in features and standard libraries. Store state must remain in memory.
- Views
- 1