Secure REST APIs
Securing Orders REST endpoints is more than attaching a JWT filter.
Introduction
Securing Orders REST endpoints is more than attaching a JWT filter. Authentication, authorization, input validation, HTTPS, error hygiene, and least privilege together form a usable baseline.
Spring Boot and Security 6 give you the knobs: SecurityFilterChain, @Valid, method security, and problem-detail error bodies that do not leak stack traces.
Never trust client-supplied identifiers for ownership. Rebind to authentication.getName() (or a custom principal id) inside the service when loading or mutating orders.
This lesson is the checklist that turns earlier mechanisms into a coherent Orders API posture.
Purpose of this lesson
Apply a practical secure-by-default checklist to TechLearningPRO Orders REST endpoints.
Understanding the topic
Layered controls beat a single filter.
- TLS everywhere: terminate HTTPS; redirect or refuse plain HTTP in production.
- Authenticate by default: permit only login, refresh, and health.
- Authorize per action: roles + ownership.
- Validate inputs:
@Valid, size limits, positive quantities. - Safe errors: stable problem codes; no SQL or stack traces to clients.
Step-by-step explanation
- Lock the
SecurityFilterChain: STATELESS + JWT + authenticated Orders routes. - Add
@Validon request bodies and constrain path ids. - Bind mutations to the principal inside
OrderService. - Verify 401/403/400 paths return sanitized JSON bodies.
Informative example
Secure create order handler shape.
@RestController@RequestMapping("/api/orders")public class OrderController {private final OrderService orders;public OrderController(OrderService orders) {this.orders = orders;}@PostMapping@ResponseStatus(HttpStatus.CREATED)OrderView create(@AuthenticationPrincipal Jwt jwt,@Valid @RequestBody CreateOrderRequest body) {String customerId = jwt.getSubject();return orders.place(customerId, body);}}public record CreateOrderRequest(@NotBlank @Size(max = 64) String sku,@Positive int qty) {}
Real-world use
An Orders mobile build once sent customerId in the JSON body. Attackers changed it and placed orders on other accounts' history views. Removing the field and using the JWT sub inside the service eliminated an entire class of insecure direct object references.
Security implications
REST security is cumulative.
- Broken object-level authorization is common — test it.
- Rate-limit auth and write endpoints.
- Strip sensitive headers from access logs.
Best practices
- Checklist against OWASP API Top 10 during design reviews.
- Centralize security response handling for 401/403.
- Keep DTOs minimal — only fields the caller needs.
Common mistakes
- Returning entity graphs that expose other users' fields.
- Verbose exception messages that reveal schema or user existence.
- CSRF disabled while still using cookie session auth for browsers.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionName three controls for a secure REST API.+
Answer
2IntermediateQuestionWhy avoid trusting client user ids?+
Answer
3BeginnerQuestionWhat does STATELESS buy you on REST APIs?+
Answer
Hands-on exercise
Lab. Implement create-order using JWT subject as customer id. Attempt to pass a different customer id in the body (field ignored or rejected) and confirm the persisted owner matches the token.
Summary
Secure Orders REST APIs combine earlier Spring Security pieces into one posture. Next: broader API protection strategies beyond auth.